Download System.out.println()

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS1101X:
Programming Methodology
Recitation 4
Design Issues and
Problem Solving
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 #4
2
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 #4
3
Required Classes
OverdueChecker
Scanner
BookTracker
LibraryBook
helper class
CS1101X Recitation #4
4
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 #4
5
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 #4
6
Step 1: Code
Directory:
Chapter7/Step1
Source Files: LibraryBook.java
Step1Main.java (test program)
CS1101X Recitation #4
7
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
private
private
private
GregorianCalendar dueDate;
String title;
double chargePerDay;
double maximumCharge;
CS1101X Recitation #4
8
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 #4
9
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 #4
10
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 #4
11
Step 1: Code LibraryBook (5/5)
// Returns the string representation of this book in the
// format <title> <charge per day> <max chanrge> <due date>
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 #4
12
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 #4
13
Step 1: Code Step1Main (2/2)
dueDate = new GregorianCalendar(2004,
Calendar.JANUARY, 12);
book3
= new LibraryBook(dueDate, 1.00, 100.00);
book3.setTitle("Java for Smarties");
dueDate = new GregorianCalendar(2004,
Calendar.JANUARY, 1);
book4
= new LibraryBook(dueDate, 1.50, 230.00,
"Me and My Java");
System.out.println(book1);
System.out.println(book2);
System.out.println(book3);
System.out.println(book4);
}
}
CS1101X Recitation #4
14
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 #4
15
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 #4
16
MyTriangle.java (2/7)
// Data members
private double sideX, sideY,
private int triangleType; //
//
private boolean isRight; //
sideZ; // the 3 sides
type: 0 = equilateral;
1 = isosceles; 2 = scalene
Right triangle?
// Constructor
public MyTriangle(double x, double y, double z) {
sideX = x;
sideY = y;
sideZ = z;
}
CS1101X Recitation #4
17
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 #4
18
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 #4
19
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 #4
20
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 #4
21
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 #4
22
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: -300
-300 = -1 * 2 * 2 * 3 * 5 * 5
Enter n: 77
77 = 1 * 7 * 11
CS1101X Recitation #4
23
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++;
}
Note: This code does not
System.out.println();
work for negative value of
n. Correct it.
CS1101X Recitation #4
24
Task: Factorisation (3/4)
Modular programming.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();
start(n);
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++;
But this method is not
}
cohesive. (Why?)
System.out.println();
}
CS1101X Recitation #4
25
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;
}
CS1101X Recitation #4
Now this is cohesive.
26
End of Recitation #4
CS1101X Recitation #4
27
Related documents