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); }
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); }
Basic Input import java.util.Scanner; public class test4 { public static void 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); }
Logic import java.util.Scanner; public class test4 { public static void 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)); }
Loops public class test4 { public static void main(String[] args) { for (int i = 0; i < 10; i++) System.out.println("The square root of " + i + " is " + Math.sqrt(i)); }
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"); }
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
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
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).)
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() {