120 likes | 214 Vues
CSE 1341 Honors. Note Set 04 Professor Mark Fontenot Southern Methodist University. Quick Review. Variables Used to store information that is going to be manipulated during the running of the program Variables must be declared before they are used Declaration: dataType variableName ;
E N D
CSE 1341 Honors Note Set 04 Professor Mark Fontenot Southern Methodist University
Quick Review • Variables • Used to store information that is going to be manipulated during the running of the program • Variables must be declared before they are used • Declaration: dataTypevariableName; • dataTypecan be int, float, double, short, long, byte, boolean, char, String (for now). • Don’t forget the rules for naming variables… What are they??? • Use the = (assignment) operator to assign a value to a variable.
Patterns to Notice So Far… • Java is Case Sensitive • Some words have special meanings • Keywords – words/identifiers that are reserved for use by the language • i.e. class, public, int (and other data types), etc. • Complete list on pg 53 of HFJava • Can’t be use as regular (programmer-declared) variable names • Statements end in semicolons public static void main (String [] args) { //not a stmt System.out.println(“Hello ”); //stmt System.out.println(“There”); //stmt } //not a stmt
Simple Math • Perform basic arithmetic in code using +, -, *, /, % • +, -, *, / - Standard Meaning • % - modulus operator – remainder of division. • comes in handy in many different situations • Follows PEMDAS rules int var1, var2; var1 = 10; var2 = var1 + 5; var1 = (3 + var2) * 2;
Integer vs. Floating Point Math • Integer arithmetic • result can only be an integer • 3 / 4 = ??? • Floating point arithmetic • result can have fractional portion • 3.0 / 4.0
Integer vs. Floating Point • How can you tell the difference? • Lower precision values are converted to higher precision values • 3.0 / 4 • 4 is an int • 3.0 is a double • therefore, 4 automatically converted to a double Standard Promotions: Double Float Long Int Short (There are actually many more. We’ll start with these.)
Convert F to C public class FtoC { public static void main (String [] args) { double fDeg; double cDeg; fDeg = 56; cDeg = (fDeg - 32) * (5.0/9.0); System.out.print("Conversion: " + fDeg+" F equals "); System.out.println(cDeg + " C."); } }
The Java API • Large “library” of code • Can be used in your programs • Organized in “packages” like java.lang, java.util, etc. • Need to “import” the appropriate package for your task. • java.lang • automatically imported into every program • contains many of the basic features that are not part of the core Java language.
Simple Input From The Keyboard • Use an object called Scanner to be able to read from the keyboard • Scanner is in the java.util package. import java.util.*; //gives access to the java.util package public class TestClass { public static void main(String [] args) { Scanner kb = new Scanner(System.in); int value; value = kb.nextInt(); //reads an int after //user presses <enter> //and so on...
F to C conversion with Keyboard input. import java.util.*; public class FtoCScanner { public static void main (String [] args) { Scanner kb = new Scanner (System.in); double degF; double degC; System.out.print("Please enter a temp in F: "); degF = kb.nextDouble(); degC = (degF - 32) * (5.0/9.0); System.out.print(degF + "f "); System.out.println("is equal to " + degC + "c."); } …more features of Scanner to come in the future…