1 / 12

Java Syntax

Java Syntax. Basic Output. public class test1 { public static void main(String[] args) { System.out.println("Hello"); } }. Basic Output. public class test1 { public static void main(String[] args) { int x = 3; System.out.println(x + " times three is " + x * 3); } }.

jmccarthy
Download Presentation

Java Syntax

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. Java Syntax

  2. Basic Output public class test1 { public static void main(String[] args) { System.out.println("Hello"); } }

  3. Basic Output public class test1 { public static void main(String[] args) { int x = 3; System.out.println(x + " times three is " + x * 3); } }

  4. Basic Math – note the library import java.math.*; public class test1 { public static void main(String[] args) { int x = 3; char y = 'a'; String word = "hello"; double z = Math.PI; double pow = Math.pow(x, 3.3); System.out.println("PI is " + z); System.out.println("Square root of " + x + " is " + Math.sqrt(3)); System.out.println("Does this work? " + z + word); System.out.println("What does this do? "+ Math.sin(x)); System.out.println("What does this do? "+ Math.sin(0)); System.out.println("What does this do? "+ Math.cos(Math.PI)); System.out.println("Power is " + pow); } }

  5. Basic Input import java.util.Scanner; publicclass test4 { publicstaticvoid main(String[] args) { Scanner data = new Scanner(System.in); System.out.println("Please enter a variable"); double var = data.nextDouble(); System.out.println("The square root is " + Math.sqrt(var)); String word = data.next(); System.out.println("The word was " + word); } }

  6. Logic import java.util.Scanner; publicclass test4 { publicstaticvoid main(String[] args) { Scanner data = new Scanner(System.in); System.out.println("Please enter a variable"); double x = data.nextDouble(); if (x < 0 ) System.out.println("Next time, please enter a non-negative value!"); else System.out.println("The square root is " + Math.sqrt(x)); } }

  7. Loops publicclass test4 { publicstaticvoid main(String[] args) { for (int i = 0; i < 10; i++) System.out.println("The square root of " + i + " is " + Math.sqrt(i)); } }

  8. More Loops import java.util.Scanner; public class test4 { public static void main(String[] args) { Scanner data = new Scanner(System.in); String answer="yes"; while(answer.equals("yes")) { System.out.println("Please enter a variable"); double x = data.nextDouble(); if (x < 0 ) System.out.println("Next time, please enter a non-negative value!"); else System.out.println("The square root is " + Math.sqrt(x)); System.out.println("Do you want to try again?"); answer = data.next(); } System.out.println("This is the end"); } }

  9. Homework First, let’s roll some dice: public class RollTheDice { public static void main(String[] args) { int die1; // The number on the first die. int die2; // The number on the second die. int roll; // The total roll (sum of the two dice). die1 = (int)(Math.random()*6) + 1; die2 = (int)(Math.random()*6) + 1; roll = die1 + die2; System.out.println("The first die comes up " + die1); System.out.println("The second die comes up " + die2); System.out.println("Your total roll is " + roll); } // end main() } // end class

  10. Now, let’s roll them! public class SnakeEyes { public static void main(String[] args) { int die1, die2; // The values rolled on the two dice. int countRolls; // Used to count the number of rolls. countRolls = 0; do { die1 = (int)(Math.random()*6) + 1; // roll the dice die2 = (int)(Math.random()*6) + 1; countRolls++; // and count this roll } while ( die1 != 1 || die2 != 1 ); System.out.println("It took " + countRolls + " rolls to get snake eyes."); } // end main() } // end class

  11. Why the Or? We want to stop rolling the dice when the roll is a double 1. We want to continue rolling the dice while the roll is not a double 1. If die1 and die2 are variables representing the values of the dice, the condition for continuing to roll can be expressed as while ( ! (die1 == 1 && die2 == 1) ) The exclamation point means "not", so the condition says that it is not the case that both die1 is 1 and die2 is 1. That is, it is not the case that the dice came up snake eyes. Another way to express the same condition is that at least one of the dice is not 1, that is, that either die1 is not 1 or die2 is not 1. In java code, this is written: while ( die1 != 1 || die2 != 1 ) This is the test that I use in my program. Students often get the && and || operators mixed up, especially when negation is involved. (In this case, we could have avoided the problem by testing while (die1+die2 != 2).)

  12. Functions public class dice { public static int Roll1() { int die = (int)(Math.random()*6) + 1; return die; } //end Roll1 public static int Roll2() { return Roll1() + Roll1(); } //end Roll2 public static void main(String[] args) } int x = dice.Roll1(); int y = dice.Roll2(); System.out.println("The first die comes up " + x); System.out.println("Two Dice are " + y); } // end main() {

More Related