1 / 12

CSE 1341 Honors

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 ;

jewell
Télécharger la présentation

CSE 1341 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 Honors Note Set 04 Professor Mark Fontenot Southern Methodist University

  2. 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.

  3. 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

  4. 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;

  5. 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

  6. 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.)

  7. 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."); } }

  8. Breakout 1 & 2

  9. 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.

  10. 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...

  11. 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…

  12. Breakout 3

More Related