Download presentation
Presentation is loading. Please wait.
Published byCecilia Leonard Modified over 9 years ago
1
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian
2
About me Email: ttian@kean.eduttian@kean.edu Office: HH-217 Office Hour: Monday, Wednesday 2:30 – 4:30 PM Tuesday, Thursday 3:15 – 5:00 PM Website: TBA
3
About the Course Mondays, Wednesdays 12:30 PM – 2:25 PM Textbook (Chapter 6 - ) Grading: Midterm Exam (7 th Week)30% Final Exam30% Lab Assignments and Homework40% Be close to the computers and try out the example programs! You can work as a team but don’t copy codes.
4
About Eclipse Free Download: http://www.eclipse.org/downloads/ 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.
5
Review of CPS 1231 Variable and data type If/ if.. else statement Switch statement While/do-while loop For loop Methods
6
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.
7
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);
8
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);
9
Now let’s try it in Eclipse.
10
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”;
11
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”); }
12
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!”); }
13
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);
14
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.
15
Exercise 2: Number Guessing Game Revise Exercise 1, so that the user can keep entering (guessing) the number until he/she is right.
16
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.
17
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
18
Write a test program that invokes these methods to display the following tables: Celsius FahrenheitFahrenheitCelsius 40105.012048.89 39102.211043.33 …… 3289.6405.44 3187.830-1.11
19
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.