Slide 1

Slide 1 text

jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 03: Programming with Java II Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Midterm Exam 1

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3 jgs Day 8 int x = 75; y = 95; z = 1; if (x < y && x < z) { if (y < z) System.out.println( x + " " + y + " " + z ); else System.out.println( x + " " + z + " " + y ); } else if (x > y && x > z) { if (y < z) System.out.println( y + " " + z + " " + x ); else System.out.println( z + " " + y + " " + x ); } else { if (y < z) System.out.println( y + " " + x + " " + z); else System.out.println( z + " " + x + " " + y); }

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs Day 9 public class Example { public static void main(String args[]) { int age = 18; switch (age) { case 16: System.out.println("You are under 18."); break; case (18): System.out.println("You are eligible for vote."); break; case (65): System.out.println("You are senior citizen."); break; default: System.out.println("Please give the valid age."); break; } } }

Slide 5

Slide 5 text

jgs Note about Strings

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6 jgs References Robert 4 variable object

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7 jgs Comparing Strings § Remember that a character string in Java is not a primitive data type. § We cannot use the relational operators to compare strings § Do NOT do this String str1 = "apple"; String str2 = "banana"; if (str1 == str2){//we are not supposed to do this }

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 jgs Comparing Strings String str1 = "apple"; String str2 = "banana"; //do NOT do this if (str1 == str2){ } // Instead, we need to do: if (str1.equals(str2)){ }

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs Comparing Strings § The following does not also compare two strings: String str1 = "apple"; String str2 = "banana"; //do NOT do this if (str1 < str2){ } // Instead, we need to do: if (str1.compareTo(str2) < 0){ }

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs Example int result = str1.compareTo(str2); if (result < 0) { //if str1 is smaller than str2 } else if (result > 0){ //if str1 is larger than str2 } else if (result == 0) //if str1 is identical to str2 }

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs Comparing Strings § Comparing strings is based on a character set, it is called a lexicographic ordering § It uses Unicode of each character in strings. § Also, short strings come before longer strings with the same prefix (lexicographically) Therefore "book" comes before "bookcase"

Slide 12

Slide 12 text

jgs Midterm Exam 2

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs Midterm 2

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 14 jgs Day 10 switch statement 1. Ask the user for a number 1 to 3 2. If the number is not 1, 2, or 3 show an error message “invalid input” and ask again. 3. If number 1 print “stone” 4. If number 2 print “paper” 5. If number 3 print “scissor” Operator ? § Ask for two integer numbers (a and b) § Using operator ?, store the highest in a new variable.

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs Day 11 while and do-while statements 1. Ask the user for a number (1 to 10) 2. If the numbers are not 1 to 10 show an error message “invalid input” and ask again. 3. Draw a square using the two numbers as length. For instance, if the user input 3, draw *** *** ***

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16 jgs Day 12 for statements In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,…

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17 jgs Day 13 Conditions and loops Draw this pattern in the screen. Example 6 x 20 0***|0***|0***|0**** *0**|*0**|*0**|*0*** **0*|**0*|**0*|**0** ***0|***0|***0|***0* ****0****0****0****0 ****|0***|0***|0****

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18 jgs Day 15 Object-oriented programming 1. Create a class Box 2. Include a variables: length, width, and height 3. Create a method that return the volume of the box, called calculateVolume() 4. Create a class MyProgram that have a method main(). It creates two boxes, put values to their dimensions, and print their volumes calling calculateVolume()

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19 jgs Day 16 Classes and Objects Create the classes needed to make this line work: public class Exam{ public static void main (String []a) { Doctor doc = new Doctor(); Doctor.color = "white”; doc.name = "Dr. Who”; doc.age = 90; doc.writePrescription(); // This is your prescription doc.hello(); //print name, age, and color. Doctor.hi(); } }

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20 jgs Day 17 Methods Create the classes needed to make this line work: public class Exam{ public static void main (String []a) { Genius g = new Genius(); double x = g.areaOfCirleWithRadius(10); double x = g.areaOfTriangle(10,20); Genius.printFirst5FibonacciNumbers(); Genius.printFirst9FibonacciNumbers(); } }

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21 jgs Questions

Slide 22

Slide 22 text

jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University. They cannot be distributed or used for another purpose.