CPS 2231 Computer Organization and Programming

Download Report

Transcript CPS 2231 Computer Organization and Programming

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
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.
•
•
•
•
•
•
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.
Review of CPS 1231
•
•
•
•
•
•
Variable and data type
If/ if.. else statement
Switch statement
While/do-while loop
For loop
Methods
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.
•
•
•
•
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);
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);
Now let’s try it in Eclipse.
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”;
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”);
• }
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!”);
• }
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);
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.
Exercise 2: Number
Guessing Game
• Revise Exercise 1, so that the user can keep entering
(guessing) the number until he/she is right.
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.
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
• 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
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.