19
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Embed Size (px)

Citation preview

Page 1: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

CPS 2231 Computer Organization

and Programming

Instructor: Tian (Tina) Tian

Page 2: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

About me• Email: [email protected]• Office: HH-217• Office Hour: Monday, Wednesday 2:30 – 4:30 PM• Tuesday, Thursday 3:15 – 5:00 PM• Website: TBA

Page 3: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

About the Course• Mondays, Wednesdays 12:30 PM – 2:25 PM• Textbook (Chapter 6 - )• Grading: Midterm Exam (7th Week)

30%• Final Exam

30%• Lab Assignments and Homework

40%• Be close to the computers and try out the

example programs!• You can work as a team but don’t copy codes.

Page 4: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

About Eclipse• Free Download: http://www.eclipse.org/downloads

/• You may need Java JDK or Java JRE.• No installation is needed.• You can save it under “Program Files” and create

a desktop shortcut.

Page 5: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Review of CPS 1231• Variable and data type• If/ if.. else statement• Switch statement• While/do-while loop• For loop• Methods

Page 6: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Variable• int x;• int x = 1;• final double PI = 3.14;• Use lowercase for variables and methods. E.g.,

numberOfStudents.• Capitalize the first letter of each word in a class

name. E.g., ComputeArea.• Capitalize every letter in a constant. E.g.,

MAX_VALUE.

Page 7: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Data Type• Numeric data types: byte, short, int, long, float,

double• char letter = ‘A’;• boolean flag = true;• String message = “Welcome to CPS 2231”;• Converting string to numbers:• String s = “123”;• int value = Integer.parseInt(s);

Page 8: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Input and Output• import java.util.Scanner;

• Scanner scanner = new Scanner(System.in);• int value = scanner.nextInt();• String s = scanner.next();

• System.out.println(“You just entered “ + s);

Page 9: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Now let’s try it in Eclipse.

Page 10: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Selection Statement• if (score >= 90)• grade = “A”;• else if(score >= 80)• grade = “ B”;• else if (score >= 70)• grade = “ C”;• else if (score >= 60)• grade = “ D”;• else• grade = “ F”;

Page 11: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Selection Statement• switch (option) {• case 0: System.out.println(“taxes for single filers”);• break;• case 1: System.out.println(“taxes for married file

jointly”);• break;• case 2: System.out.println(“taxes for married file

seperately”);• break;• default: System.out.println(“Invalid status”);• }

Page 12: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Loops• int count = 0;• while (count < 100) {• System.out.println(“Welcome to Java!”);• count++;• }

• for (int i = 0; i< 100; i++) {• System.out.println(“Welcome to Java!”);• }

Page 13: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Methods• public static String grading (double score) {• String grade = “”; • if (score >= 90)• grade = “A”;• else if(score >= 80)• grade = “B”;• else if (score >= 70)• grade = “C”;• else if (score >= 60)• grade = “D”;• else• grade = “F”;• return grade;• }

• String grade = grading(83);

Page 14: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Exercise 1: Number Guessing Game

• Write a program that generate an integer between 0 and 10 and prompts the user to enter (guess) this number. The program then reports true if the answer is correct, false otherwise.

Page 15: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Exercise 2: Number Guessing Game

• Revise Exercise 1, so that the user can keep entering (guessing) the number until he/she is right.

Page 16: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Exercise 3: Finding the Highest Score

• Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score.

Page 17: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Exercise 4: Conversions between Celsius and

Fahrenheit• Write a class that contains the following two

methods:• public static double celsiusToFahrenheit (double

celsius)• public static double fahrenheitToCelcius(double

fahrenheit)

• The following formula for the conversion is:• fahrenheit = (9.0/5)*celsius + 32

Page 18: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

• Write a test program that invokes these methods to display the following tables:

• Celsius Fahrenheit Fahrenheit Celsius• 40 105.0 120 48.89• 39 102.2 110 43.33• … …• 32 89.6 40 5.44• 31 87.8 30 -1.11

Page 19: CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian

Exercise 5: Display an Integer Reversed

• Write the following method to display an integer in reverse order:

• public static void reverse (int number)

• For example, reverse(3456) displays 6543.