Discussion 5. Lab 3 Not really using rectangle class Return type of getPoint is String instead of Point You are not able to retrieve the point if you.

Slides:



Advertisements
Similar presentations
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
Chapter Chapter 8 Exceptions and Assertions.
Chapter 2 Review Questions
Discussion 3. Questions? Term test? Lectures? Labs?
Chapter 4 Loops Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 6 Repetition Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions and Assertions Animated Version.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Chapter Chapter 6 Repetition Statements. Objectives Understand repetition control (loop ) statements in Java: while statement. do-while statement.
Chapter 7- 1 Chapter 7 Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
CS1101X: Programming Methodology Recitation 3 Control Structures.
CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 Defining Your Own Classes Part 2 Animated Version.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2 (adapted.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 Defining Your Own Classes Part 2.
Written by: Dr. JJ Shepherd
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Inheritance and Polymorphism.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lesson #5 Repetition and Loops.
Lesson #5 Repetition and Loops.
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Defining Your Own Classes
Lesson #5 Repetition and Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Introduction to javadoc
Lesson #5 Repetition and Loops.
Chapter 7 Defining Your Own Classes Part 2
Presentation transcript:

Discussion 5

Lab 3 Not really using rectangle class Return type of getPoint is String instead of Point You are not able to retrieve the point if you want (of course some of you did extra methods like getPoint1X() etc..) (but this in a way is violating the requirements of the question and a very clumsy way of coding)

Lab 3 Not having a toString() method Generally this is preferred when there ’ s a need to display the content of the object(String representation) Not setting the type in the constructor and the setPoint methods What ’ s the use for private int rectangleType;

Lab 3 public void setType( int i ) -> private … Accessors becoming mutator getType() you set the set type in here Misunderstanding rectangleType Some of you thought it is for you to use inside your method

Lab3 In OO programming, when it comes to declaring classes, the variables inside the class should describe the class e.g class Human{ String haircolor; int age; //etc } you don't declare extra variables inside the class so that your methods can use it i.e supposed you got a getBMI() method that finds the body mass index, and you need some extra variables to store some results which will be used inside the method for other computation, you declare all these extra variables inside getBMI() method itself this will be invisible to other methods

Questions about Lab4/Lectures? You are not supposed to change Peg.java

Exercise 6 q 8 A prime number is an integer greater than 1 and divisible by only itself and 1. The first seven prime numbers are 2,3,5,7,11,13 and 17. Write a method that returns true if its parameter is a prime number

Problem Statement Write an application that computes the total charges for the overdue library books. For each library book, the user enters the due date and (optionally) the overdue charge per day,the maximum charge, and the title. If the optional values are not entered, then the preset default values are used. A complete list of book information is displayed when the user finishes entering the input data.The user can enter different return dates to compare the overdue charges.

Overall Plan Tasks: 1. Get the information for all books 2. Display the entered book information 3. Ask for the return date and display the total charge. Repeat this step until the user quits.

Required Classes OverdueChecker Scanner LibraryBook helper class BookTracker

Development Steps We will develop this program in five steps: 1. Define the basic LibraryBook class. 2. Explore the given BookTracker class and integrate it with the LibraryBook class. 3. Define the top-level OverdueChecker class. Implement the complete input routines. 4. Complete the LibraryBook class by fully implementing the overdue charge computation. 5. Finalize the program by tying up loose ends.

Step 1 Design Develop the basic LibraryBook class. The key design task is to identify the data members for storing relevant information. We will include multiple constructors for ease of creating LibraryBook objects. Make sure that an instance will be initiated correctly no matter which constructor is used.

Step 1 Code Directory: Chapter7/Step1 Source Files: LibraryBook.java Step1Main.java (test program) Directory: Chapter7/Step1 Source Files: LibraryBook.java Step1Main.java (test program) Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.

Step 1 Test In the testing phase, we run the test main program Step1Main and confirm that we get the expected output:

Step 2 Design Explore the helper BookTracker class and incorporate it into the program. Adjust the LibraryBook class to make it compatible with the BookTracker class.

Step 2 Code Directory: Chapter7/Step2 Source Files: LibraryBook.java Step2Main.java (test program) Directory: Chapter7/Step2 Source Files: LibraryBook.java Step2Main.java (test program)

Step 2 Test In the testing phase, we run the test main program Step2Main and confirm that we get the expected output. We run the program multiple times trying different variations each time.

Step 3 Design We implement the top-level control class OverdueChecker. The top-level controller manages a single BookTracker object and multiple LibraryBook objects. The top-level controller manages the input and output routines If the input and output routines are complex, then we would consider designing separate classes to delegate the I/O tasks.

Step 3 Pseudocode GregorianCalendar returnDate; String reply, table; double totalCharge; inputBooks(); //read in all book information table = bookTracker.getList(); System.out.println(table); //try different return dates do { returnDate = read return date ; totalCharge = bookTracker.getCharge(returnDate); displayTotalCharge(totalCharge); reply = prompt the user to continue or not; } while ( reply is yes );

Step 3 Code Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java

Step 3 Test Now we run the program multiple times, trying different input types and values. We confirm that all control loops are implemented and working correctly. At this point, the code to compute the overdue charge is still a stub, so we will always get the same overdue charge for the same number of books. After we verify that everything is working as expected,we proceed to the next step.

Step 4: Compute the Charge To compute the overdue charge, we need two dates: the due date and the date the books are or to be returned. The getTimeInMillis method returns the time elasped since the epoch to the date in milliseconds. By subtracting this since-the-epoch milliseconds value of the due date from the same of the return date, we can find the difference between the two. If the difference is negative, then it ’ s not past due, so there ’ s no charge. If the difference is positive, then we convert the milliseconds to the equivalent number of days and multiply it by the per- day charge to compute the total charge.

Step 4 Code Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java Directory: Chapter7/Step3 Source Files: OverdueChecker.java LibraryBook.java

Step 4 Test We run the program mutiple times again, possibly using the same set of input data. We enter different input variations to try out all possible cases for the computeCharge method. Try cases such as the return date and due date are the same, the return date occurs before the due date, the charge is beyond the maximum, and so forth. After we verify the program,we move on to the next step.

Step 5: Finalize / Extend Program Review Are all the possible cases handled? Are the input routines easy to use? Will it be better if we allow different formats for entering the date information? Possible Extensions Warn the user, say, by popping a warning window or ringing an alarm, when the due date is approaching. Provide a special form window to enter data (Note: To implement these extensions, we need techniques not covered yet.)