Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;

Similar presentations


Presentation on theme: "CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;"— Presentation transcript:

1 CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: http://twiki-edlab.cs.umass.edu/bin/view/Moll121/WebHome Owl due Friday; another one coming. Second programming assignment now up, due in class, on paper, next Tuesday Read ch 3 through section 3.9 for thursday MidTerm: Monday, 10/18, 615-715, Thompson 102,106 (I’ll say more later..) Office hours - now posted; held in LGRT 220 I will accept late programming assignments on Thursday

2 Java’s Object Model Classes - a class is a blueprint or template for an object an Object is an example or an instantiation of a class definition an instance variable or attribute is a characteristic of an Object a method is an activity that serves an object Example: A Student class is a template for a student Attributes - name, age, credits, year, etc Methods - getAge, creditsLeft, calcGPA, and so forth Writing a class definition prepares this template Different students mean different objects -- with different attribute values!

3 Conditional Statements in Java Conditional statements (and looping statements) are flow of control constructions At a primitive level, Java programs are made up of statements, and it often makes sense to 1)have statements repeat in a systematic way; and 2)have statements execute conditionally

4 if (n % 2 == 0) System.out.println(“n is even”); Lots going on here: statement says: “if the remainder after dividing n by 2 is equal to (==) 0, then report that n is an even number if (n % 2 != 0) System.out.println(“n is odd”); else System.out.println(“n is even”); An important point: (n % 2 == 0) is a boolean expression (returns a boolean value) -- a boolean must go into the test slot of an if stmt!

5 The for loop for(int j = 0; j j = j + 1 System.out.println(“I will not talk in class”); } for(int j = 3; j < 12; j = j + 2){ System.out.println(j);} // prints what? for(int j = 30; j > 20; j = j - 2){ System.out.println(j);} // prints what?

6 for(int j = 3; j < 12; j = j*j){ System.out.println(j);} // prints what? for(int j = 3; j != 12; j = j + 2){ System.out.println(j);} // prints what?

7 The while loop An especially flexible construction int k = 0; while (k < 100){ System.out.println(k); k = k + 1; } // (or: k++;) Note also-> 2 stmt body int k = 0; while (k < 100){ System.out.println(k); k = 2*k; } int k = 1; while (k < 100){ System.out.println(k); k = 2*k; }

8 public class Rows{ final char Blank = ' '; // a constant! public void makeRow(int k, char sym){ // prints k symbols (sym) in a row for(int j = 0; j < k; j++) System.out.print(sym); } public void newLine(){System.out.println();}// goes to new line public void spacedRow(int k, char sym){ // prints k symbols in a row, every other on is blank for(int j = 0; j < k; j++) if (j % 2 == 0) System.out.print(sym); else System.out.print(Blank); } }

9 public class RowTester{ public static void main(String[] args){ Rows r = new Rows(); for(int j = 0; j < 5; j++){ r.makeRow(10,'*'); r.newLine(); } } } This program makes 5 rows of 10 stars: *********** etc

10 public class RowTester{ public static void main(String[] args){ Rows r = new Rows(); for(int j = 0; j < 5; j++){ r.makeRow(j+2,'*'); r.newLine(); } } }

11 The result: ** *** **** ***** ******

12 Random numbers Java has a Random class (in the java.util package) It produces pseudo-random numbers Random r = new Random(); vs Random r = new Random(303); r.nextInt(); vs r.nextInt(1000 r.nextDouble() -> produces a random value between 0 and 1.0

13 import java.util.Random; public class RandomTester{ public static void main(String[] args) { Random r1 = new Random(202); Random r2 = new Random(303); System.out.println(" from r1: " + r1.nextInt()); System.out.println(" from r2: " + r2.nextInt()); } } œ from r1: -1150098092 œ from r2: -1168181290

14 import java.util.Random; public class RowTester{ public static void main(String[] args){ Random randy = new Random(); Rows r = new Rows(); int oddSize; for(int j = 0; j < 5; j++){ oddSize = randy.nextInt(40); r.makeRow(oddSize,'*'); r.newLine(); }

15 Result of one run: ************************** **** ********* ****** *********************************** Result of a second run: ************************************** ************ ******************* *****

16 A real use for random numbers (more or less..) We know that √2 = ~ 1.414 This says that if I choose 1000 numbers between 1 and 2, about 414 should fall below √2 -- the others should be above this value. We can actually use this (probabilistic) technique to compute this values (unimportant) and other values (important!) Here’s the code…

17 import java.util.Random; public class RootTwo{ public static void main(String[] args){ Random r = new Random(); int ct = 0; double val; for(int j = 0; j < 1000; j++){ val = r.nextDouble(); if ((1 + val) * (1 + val) < 2) ct++; } System.out.println("Square root of 2 is " + (1 + (double)ct/1000)); } } jGRASP exec: java RootTwo œœßœSquare root of 2 is 1.408


Download ppt "CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;"

Similar presentations


Ads by Google