Contents
- ./Conversion.java
- ./EscapeCharacters.java
- ./Float.java
- ./WinPercentage.java
./Conversion.java 1/4
[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
System.out.println("There are " + numCM + " cm in " + numInches + " inches.");
}
}
./EscapeCharacters.java 2/4
[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/4
[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);
}
}
./WinPercentage.java 4/4
[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!");
}
}
Generated by GNU Enscript 1.6.5.90.