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

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 Repetition structures Overview while statement for statement do while statement.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Review if Online Time For loop Quiz on Thursday.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
CS001 Introduction to Programming Day 6 Sujana Jyothi
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
AP Java Java’s version of Repeat until.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Day Programming with Methods. The “do” loop The “do” loop is a type of while loop that only does one iteration (does one loop at least) It is hardly.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Chapter 2 Clarifications
CSC111 Quick Revision.
Software Development I/O and Numbers
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
5.1 Programming with Methods
Computer Programming Methodology Input and While Loop
Repetition-Counter control Loop
Repetition.
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
Introduction to Computer Programming
Truth tables: Ways to organize results of Boolean expressions.
Building Java Programs
Truth tables: Ways to organize results of Boolean expressions.
Scope of variables class scopeofvars {
Building Java Programs
Building Java Programs
Indefinite loop variations
Random Numbers while loop
More on iterations using
Computer Science Club 1st November 2019.
Optional Topic: User Input with Scanner
Presentation transcript:

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() {