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

Slides:



Advertisements
Similar presentations
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Advertisements

Review BP Dari slide pak cahyo pertemuan 7 dan pertemuan 2 dengan sedikit modifikasi.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
1 Various Methods of Populating Arrays Randomly generated integers.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Arrays Chapter 6 Chapter 6.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
CS102--Object Oriented Programming Discussion 1: – Project 4 on Page 328 – The use of arrays Copyright © 2008 Xiaoyan Li.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Java vs. You.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
CS1101: Programming Methodology Aaron Tan.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Homework #2: Functions and Arrays By J. H. Wang Mar. 24, 2014.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Review if Online Time For loop Quiz on Thursday.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
int [] scores = new int [10];
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Arrays Chapter 7.
CSC111 Quick Revision.
Chapter 7 User-Defined Methods.
Chapter 7 Part 1 Edited by JJ Shepherd
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Java Fix a program that has if Online time for Monday’s Program
Chapter 8 Slides from GaddisText
Java Fix a program that has if Online time for Monday’s Program
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
int [] scores = new int [10];
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
CS201 Fall 2016 George Koutsogiannakis
Review for Midterm 3.
Presentation transcript:

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

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)

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.

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

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) { }

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 }

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 }

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

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--)

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?

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?

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.

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?

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.

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)