Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.

Similar presentations


Presentation on theme: "CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li."— Presentation transcript:

1 CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li

2 Project 6 on Page 409 Write a program that reads numbers from the keyboard into an array of type int[]. You may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50 numbers. – (1) Output the all numbers you have read from the largest to the smallest. – (2) Output unique values in the array from the largest to the smallest. (extra 5 points) – (3) Output a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest. (extra 5 points)

3 Two Types of Tasks Problem solving task – Design a program/class to solve a particular problem. – Usually accept user input either from the keyboard, mouse or files and have outputs to the screen or files. – Use existing class provided by java (String, Scanner, System, Arrays, Math …) or self-developed classes – Must have a main method Tool-based task – Design classes that can be used by other programmers. – Usually do not accept inputs and do not have outputs. – May use existing class provided by java (String, System, Arrays, Math …) or self-developed classes. – Main method is not necessary but you can have one in your class for testing.

4 The Structure of a Program/Class for a Program-solving Task public class MyProgram { public static void main(String[] args) { }

5 The Structure of a Program/Class for a Tool-based task public class MyClass { //instance variables //methods //optional main method for testing public static void main(String[] args) { }

6 The Structure of a Program/Class for a Tool-based task public class MyClass { //instance variables //Q1.What are the instance variables? (Student’s info vs. Person’s info) //Q2. (primitive or class type? Single variables or Arrays?) //Q3. public or private? //Q4. static or non-static? //Q5. final? (constant) //methods //optional main method for testing }

7 The Structure of a Program/Class for a Tool-based task public class MyClass { //instance variables //methods //1. constructors: regular one and a copy constructor (deep copy) //2. accessor methods/getters //3. mutator methods/setters //4. toString() method //5. equals method //6. other helping methods //optional main method for testing }

8 Task1: A student takes 5 courses this semester. write a program that read her scores from the keyboard. 1. Output the average score. 2. Output her scores from the lowest to the highest 3. Output her scores from the highest to the lowest What type of task is it? What is the general algorithm for my program? – Step1: read scores from the keyboard into an array – Step2. calculate the average score – Step3. output the average score – Step4. sort my array in ascending order – Step5. output my array from the beginning to the end – Step6. output my array from the end to the beginning Translate my algorithm into program

9 Step1: read scores from the keyboard into an array – Scanner class, double[] or int[] – the for loop Step2. calculate the average score – Access each element: for loop – A variable for sum and a variable for average – Q: can we combine Step 1 and Step 2? And how? Step3. output the average score – System.out.println Step4. sort my array in ascending order – The Arrays class (how) Step5. output my array from the beginning to the end – for (i=0; i< 5; i++)) Step6. output my array from the end to the beginning – for(i=4; i>=0; i--)

10 Search/count a target in an array int target = 5; int[] numbers = new int[20]; // assume that we have read 20 numbers into the array, T1: check whether 5 is in the array? T2: count how many 5s in the array?

11 Search/count a target in an partially filled array int target = 5; int[] numbers = new int[20]; // assume that we have asked how many numbers the user wants to input with the maximum of 20 and read those numbers into the array, – numberOfItems= keyboard.nextInt(); T1: check whether 5 is in the array? T2: count how many 5s in the array?

12 Error-checking Check whether the input is valid? – If the input value is not valid numberOfItems? – Two choices: Exit the program, or Give the user another chance – Please add error-checking for your second project. Run your program before you add error-checking. First type -5 when you are asked about how many numbers that you want to input and then try 60 to see what happens if your program does not check the input value.

13 Exercise2: (one-dimensional array or two-dimensional array?) There are 5 registered students in our class. Each takes 4 courses this semester. Write a program that read scores from the keyboard, calculate the average score for each student and output the average scores from the highest to the lowest. Hints: What are the variables that we need for this task? Where are all the scores for student i? Where is the average score for student i?

14 More exercise: Given an array of numbers, can you output the smallest number? The largest number? Two different ways: – Sort and then you know where the largest number of smallest number is? – Initialize maximum=number[0] and then compare it with the other elements in the array.

15 Announcement Programming assignment3 – Project 11 on page 411 – Due on Thursday March 6 th. Self-test exercises in Chapter 6(arrays) – Do the odd-numbered questions (1, 3,…,27) – You do not turn in your answers. – Do the exercises before you start working on the project!! Next Lecture: Inheritance (Chapter 7)


Download ppt "CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li."

Similar presentations


Ads by Google