Contents
- ./Conversion.java
- ./EscapeCharacters.java
- ./Float.java
- ./Hello.java
- ./WinPercentage.java
./Conversion.java 1/5
[top][prev][next]
/**
* This class converts from inches to centimeters.
*
* @author Sara Sprenkle
*
*/
public class Conversion {
/**
* Called when user runs
* java Conversion
*/
public static void main(String[] args) {
int numInches = 1;
double cm_per_inch = 2.540;
double numCM = numInches*cm_per_inch;
// Need to put + in between string literals and variables.
// Need to put explicit spaces into string literals.
// Note that Java will automatically convert the ints and doubles
// to Strings when you concatenate a String with them.
System.out.println("There are " + numCM + " cm in " + numInches + " inches.");
}
}
./EscapeCharacters.java 2/5
[top][prev][next]
/**
* This class demonstrates using some escape characters
*
* @author Sara Sprenkle
*/
public class EscapeCharacters {
/**
* Called when user runs
* java EscapeCharacters
*/
public static void main(String[] args) {
System.out.println("To print a \\, you must use \"\\\\\".");
System.out.println("\tTab before\tand in the middle.");
System.out.println("\nExtra blank line.");
}
}
./Float.java 3/5
[top][prev][next]
/**
* This class demonstrates how to specify floats vs doubles in Java.
*
* But, it's easier to just use doubles.
*
* @author Sara Sprenkle
*/
public class Float {
/**
* Called when user runs
* java Float
*/
public static void main(String[] args) {
float f = 3.14;
// double f = 3.14; // <-- use this solution
System.out.println(f);
}
}
./Hello.java 4/5
[top][prev][next]
/**
* Demonstrates new feature _previewed_ in Java 21.
* Previewed features may not be permanent.
*
* Compile and run using
* javac --source 21 --enable-preview Hello.java
* java --source 21 --enable-preview Hello.java
*
*
* @author Sara Sprenkle
*/
public class Hello {
void main() {
System.out.println("Hello!");
}
}
./WinPercentage.java 5/5
[top][prev][next]
/**
* Demonstrates casting a variable to a certain type
* and automatically changing a double to a String in a print statement.
*
* @author Sara Sprenkle
*/
public class WinPercentage {
public static void main(String[] args) {
int wins = 95;
int losses = 56;
int numGames = wins + losses;
/* try removing the (double) below to see what happens. */
double winPct =(double) wins/numGames;
System.out.println("Your win percentage is " + winPct);
System.out.println("Nice job!");
System.out.println("woo hoo!");
}
}
Generated by GNU Enscript 1.6.5.90.