CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.

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.
Written by: Dr. JJ Shepherd
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Discussion 3. Questions? Term test? Lectures? Labs?
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 7- 1 Chapter 7 Defining Your Own Classes Part 2.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
LAB 10.
CS1101X: Programming Methodology Recitation 2 Classes.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Computer Programming Lab(4).
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
CS1101: Programming Methodology Aaron Tan.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CS1101: Programming Methodology Aaron Tan.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CS1101: Programming Methodology
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
CS1101X: Programming Methodology Recitation 7 Arrays II.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
CS1101: Programming Methodology Recitation 3 – Control Structures.
CS1101: Programming Methodology Aaron Tan.
CS1101X: Programming Methodology Recitation 3 Control Structures.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.
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.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
Chapter 4: Control Structures II
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 5: Control Structures II
CS1101: Programming Methodology Aaron Tan.
Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2 (adapted.
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CS1101: Programming Methodology Preparing for Practical Exam (PE)
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 Defining Your Own Classes Part 2.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Written by: Dr. JJ Shepherd
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
CS1101: Programming Methodology
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
CS1101X: Programming Methodology Recitation 10 Inheritance and Polymorphism.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 5: Control Structures II
Computer Programming Methodology Input and While Loop
Chapter 5: Control Structures II
More on Classes and Objects
Building Java Programs
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving

CS1101X Recitation #42 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.

CS1101X Recitation #43 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.

CS1101X Recitation #44 Required Classes OverdueChecker Scanner LibraryBook helper class BookTracker

CS1101X Recitation #45 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.

CS1101X Recitation #46 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.

CS1101X Recitation #47 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)

CS1101X Recitation #48 Step 1: Code LibraryBook (1/5) import java.util.*; import java.text.*; class LibraryBook { // default values private static final double CHARGE_PER_DAY = 0.50; private static final double MAX_CHARGE = 50.00; private static final String DEFAULT_TITLE = "Title unknown"; private GregorianCalendar dueDate; private String title; private double chargePerDay; private double maximumCharge;

CS1101X Recitation #49 Step 1: Code LibraryBook (2/5) // Constructors: public LibraryBook(GregorianCalendar dueDate) { this(dueDate, CHARGE_PER_DAY); } public LibraryBook(GregorianCalendar dueDate, double chargePerDay) { this(dueDate, chargePerDay, MAX_CHARGE); } public LibraryBook(GregorianCalendar dueDate, double chargePerDay, double maximumCharge) { this(dueDate, chargePerDay, maximumCharge, DEFAULT_TITLE); }

CS1101X Recitation #410 Step 1: Code LibraryBook (3/5) public LibraryBook(GregorianCalendar dueDate, double chargePerDay, double maximumCharge, String title) { setDueDate(dueDate); setChargePerDay(chargePerDay); setMaximumCharge(maximumCharge); setTitle(title); }

CS1101X Recitation #411 Step 1: Code LibraryBook (4/5) // // Public Methods: // double getChargePerDay( ) // GregorianCalendar getDueDate( ) // double getMaxCharge( ) // String getTitle( ) // // void setChargePerDay(double) // void setDueDate(GregorianCalendar ) // void setMaxCharge(double) // void setTitle(String) // // String toString( ) // // actual codes omitted

CS1101X Recitation #412 Step 1: Code LibraryBook (5/5) // Returns the string representation of this book in the // format public String toString( ) { String tab = "\t"; SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); DecimalFormat df = new DecimalFormat("0.00"); return getTitle() + tab + "$ " + df.format(getChargePerDay()) + tab + "$ " + df.format(getMaxCharge()) + tab + sdf.format(dueDate.getTime()); }

CS1101X Recitation #413 Step 1: Code Step1Main (1/2) import java.util.*; class Step1Main { public static void main( String[] args ) { //Create 4 LibraryBook objects and output them GregorianCalendar dueDate, returnDate; LibraryBook book1, book2, book3, book4; returnDate = new GregorianCalendar(2004, Calendar.MARCH, 15); dueDate = new GregorianCalendar(2004, Calendar.MARCH, 14); book1 = new LibraryBook(dueDate); dueDate = new GregorianCalendar(2004, Calendar.FEBRUARY, 13); book2 = new LibraryBook(dueDate, 0.75); book2.setTitle("Introduction to OOP with Java");

CS1101X Recitation #414 Step 1: Code Step1Main (2/2) dueDate = new GregorianCalendar(2004, Calendar.JANUARY, 12); book3 = new LibraryBook(dueDate, 1.00, ); book3.setTitle("Java for Smarties"); dueDate = new GregorianCalendar(2004, Calendar.JANUARY, 1); book4 = new LibraryBook(dueDate, 1.50, , "Me and My Java"); System.out.println(book1); System.out.println(book2); System.out.println(book3); System.out.println(book4); }

CS1101X Recitation #415 Notes Note how the LibraryBook class is designed. What are its data members? How are the constructors written? How are the accessors and mutators written? Read up the subsequent steps yourself.

CS1101X Recitation #416 MyTriangle.java (1/7) // CS1101 (AY2005/6 Semester 1) // Lab 3 Task 1 // MyTriangle.java // Aaron Tan /** * This class contains selected properties of a triangle: * lengths of its sides, type (equilateral, isosceles * or scalene), and whether it is a right triangle. */ import java.util.*; class MyTriangle { // Codes for triangle types public static final int IS_EQUILATERAL = 0; public static final int IS_ISOSCELES = 1; public static final int IS_SCALENE = 2; // For checking for right triangle public static final double EPSILON = 0.001;

CS1101X Recitation #417 MyTriangle.java (2/7) // Data members private double sideX, sideY, sideZ; // the 3 sides private int triangleType; // type: 0 = equilateral; // 1 = isosceles; 2 = scalene private boolean isRight; // Right triangle? // Constructor public MyTriangle(double x, double y, double z) { sideX = x; sideY = y; sideZ = z; }

CS1101X Recitation #418 MyTriangle.java (3/7) / // Public Methods: // void setSideX ( double ); // void setSideY ( double ); // void setSideZ ( double ); // void setType ( int ); // void setIsRight ( boolean ); // double getSideX ( ); // double getSideY ( ); // double getSideZ ( ); // int getType ( ); // boolean getIsRight ( ); // // codes omitted

CS1101X Recitation #419 MyTriangle.java (4/7) // main method used to test the MyTriangle class public static void main (String [] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter 3 sides: "); double side1 = scanner.nextDouble(); double side2 = scanner.nextDouble(); double side3 = scanner.nextDouble(); // To read until a valid set of sides are entered while (side1 + side2 <= side3) { System.out.print("Enter 3 sides again: "); side1 = scanner.nextDouble(); side2 = scanner.nextDouble(); side3 = scanner.nextDouble(); }

CS1101X Recitation #420 MyTriangle.java (5/7) MyTriangle tri = new MyTriangle(side1, side2, side3); System.out.println("Sides are " + tri.getSideX() + " " + tri.getSideY() + " " + tri.getSideZ()); // Determine triangle type if (side1 == side2 && side2 == side3) { tri.setType(IS_EQUILATERAL); } else if (side1 == side2 || side1 == side3 || side2 == side3 ) { tri.setType(IS_ISOSCELES); } else { tri.setType(IS_SCALENE); }

CS1101X Recitation #421 MyTriangle.java (6/7) // Determine if triangle is right-angled // using Pythagoras' Theorem. double tempValue = Math.abs((side1 * side1 + side2 * side2) - (side3 * side3)); tri.setIsRight(tempValue <= EPSILON); // Output System.out.print("Type is "); switch (tri.getType()) { case IS_EQUILATERAL: System.out.println("equilateral."); break; case IS_ISOSCELES: System.out.println("isosceles."); break; case IS_SCALENE: System.out.println("scalene."); }

CS1101X Recitation #422 MyTriangle.java (7/7) System.out.print("Triangle "); if (tri.getIsRight()) System.out.println("is right."); else System.out.println("is not right."); }

CS1101X Recitation #423 Task: Factorisation (1/4) Past PE question Time limit: 30 minutes Write a program to read in a non-zero integer and display the factorisation. Examples: Enter n: 8 8 = 1 * 2 * 2 * 2 Enter n: = -1 * 2 * 2 * 3 * 5 * 5 Enter n: = 1 * 7 * 11

CS1101X Recitation #424 Task: Factorisation (2/4) Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); if (n > 0) System.out.print(n + " = 1"); else System.out.print(n + " = -1"); int factor = 2; while (n > 1) { if (n % factor == 0) { System.out.print(" * " + factor); } else factor++; } System.out.println(); Note: This code does not work for negative value of n. Correct it.

CS1101X Recitation #425 Task: Factorisation (3/4) Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); start(n); Modular programming. private static void start(int value) { if (value > 0) System.out.print(value + " = 1"); else System.out.print(value + " = -1"); int factor = 2; while (value > 1) { if (value % factor == 0) { System.out.print(" * " + factor); } else factor++; } System.out.println(); } But this method is not cohesive. (Why?)

CS1101X Recitation #426 Task: Factorisation (4/4) Scanner scanner = new Scanner(System.in); System.out.print("Enter n: "); int n = scanner.nextInt(); String answer = start(n); System.out.println(answer); private static String start(int value) { String ans = ""; if (value > 0) ans += value + " = 1"; else ans += value + " = -1"; int factor = 2; while (value > 1) { if (value % factor == 0) { ans += " * " + factor; } else factor++; } return ans; } Now this is cohesive.

CS1101X Recitation #427 End of Recitation #4