![]() |
||
Select a Chapter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Back to the Main Page
|
||
| Chapter Six listings: 8 classes | ||
import javax.swing.JOptionPane;
class GrowthRatesTester
{
public static void main (String[ ] args)
{ JOptionPane.showMessageDialog (null,
"Calculating growth for various interest rates");
new GrowthRates();
System.exit (0);
} //======================
} //########################################################
public class GrowthRates extends Object
{
/** Calculate time to double your money at a given rate. */
public GrowthRates()
{ String input = JOptionPane.showInputDialog
("Annual rate? 0 if done:");
double rate = Double.parseDouble (input);
while (rate > 0.0)
{ JOptionPane.showMessageDialog (null,
"It takes " + yearsToDouble (rate)
+ " years for \nyour money to double.");
input = JOptionPane.showInputDialog
("Another rate (0 when done):");
rate = Double.parseDouble (input);
}
} //======================
/** Precondition: interestRate is positive. */
public int yearsToDouble (double interestRate)
{ double balance = 1.0;
int count = 0;
while (balance < 2.0)
{ balance = balance * (1.0 + interestRate / 100.0);
count++;
}
return count;
} //======================
}
import javax.swing.JOptionPane;
public class IO
{
/** Display a message to the user of Jo's Repair Shop. */
public static void say (Object message)
{ JOptionPane.showMessageDialog (null, message,
"Jo's Repair Shop", JOptionPane.PLAIN_MESSAGE);
} //======================
/** Display the prompt to the user; wait for the user to enter
* a string of characters; return that String (not null). */
public static String askLine (String prompt)
{ String s = JOptionPane.showInputDialog (prompt);
if (s == null)
return "";
else
return s;
} //======================
/** Display the prompt to the user; wait for the user to enter
* a number; return it, or return -1 if ill-formed. */
public static double askDouble (String prompt)
{ String s = JOptionPane.showInputDialog (prompt);
return new StringInfo (s).parseDouble (-1);
} //======================
/** Display the prompt to the user; wait for the user to enter
* a whole number; return it, or return -1 if ill-formed. */
public static int askInt (String prompt)
{ String s = JOptionPane.showInputDialog (prompt);
return (int) new StringInfo (s).parseDouble (-1);
} //======================
}
public class TestOrdered
{
/** Tell what kind of order the 3 values are in.
* Precondition: All 3 are mutually comparable. */
public static String ordered (Comparable first,
Comparable second, Comparable third)
{ if (first.compareTo (second) == 0)
return (second.compareTo (third) <= 0)
? "in ascending order" : "in descending order";
else if (first.compareTo (second) < 0)
return (second.compareTo (third) > 0)
? "not in order" : "in ascending order";
else // first comes after second
return (second.compareTo (third) < 0)
? "not in order" : "in descending order";
} //=======================
}
public class StringInfo extends Object implements Comparable
{
private String itself;
public StringInfo (String given)
{ itself = (given == null) ? "" : given;
} //=======================
public String toString()
{ return itself;
} //=======================
/** The standard Comparable method.
* Precondition: ob is a StringInfo object. */
public int compareTo (Object ob)
{ return itself.compareTo (((StringInfo) ob).itself);
} //=======================
/** Remove all characters before index n, plus any
* whitespace immediately following those characters. */
public void trimFront (int n)
{ while (n < itself.length() && itself.charAt (n) <= ' ')
n++;
itself = n > itself.length() ? "" : itself.substring (n);
} //=======================
/** Return the first word of the String, down to but not
* including the first whitespace character. */
public String firstWord()
{ for (int k = 0; k < itself.length(); k++)
{ if (itself.charAt (k) <= ' ')
return itself.substring (0, k);
}
return itself; // since the string has no whitespace
} //=======================
/** Strip out all non-digits from the StringInfo object. */
public void retainDigits()
{ String result = "";
for (int k = 0; k < itself.length(); k++)
{ if (itself.charAt(k) >= '0' && itself.charAt(k) <= '9')
result += itself.charAt (k);
}
itself = result;
} //=======================
/** Return the numeric equivalent of itself.
* Ignore the first invalid character and anything after it.
* If the part of the string before the first invalid
* character is a numeral, return the double equivalent,
* otherwise return the value of badNumeral. */
public double parseDouble (double badNumeral)
{ if (itself.length() == 0)
return badNumeral;
boolean hasNoDigit = true; // until a digit is seen
int pos = (itself.charAt (0) == '-') ? 1 : 0;
for ( ; hasDigitAt (pos); pos++)
hasNoDigit = false;
if (pos < itself.length() && itself.charAt (pos) == '.')
{ for (pos++; hasDigitAt (pos); pos++)
hasNoDigit = false;
}
return hasNoDigit ? badNumeral
: Double.parseDouble (itself.substring (0, pos));
} //=======================
/** Precondition: pos >= 0. */
private boolean hasDigitAt (int pos)
{ return pos < itself.length() && itself.charAt (pos) >= '0'
&& itself.charAt (pos) <= '9';
} //=======================
}
import javax.swing.*;
public class PrintSquares
{
/** Print a table of integers 1..10 and their squares. */
public PrintSquares()
{ JTextArea area = new JTextArea (10, 15); //1
JScrollPane scroller = new JScrollPane (area); //2
String table = "Table of integers and their squares"; //3
for (int k = 1; k <= 100; k++) //4
table += "\n" + k + "\t" + k * k; //5
area.append (table); //6
JOptionPane.showMessageDialog (null, scroller); //7
} //======================
} //########################################################
class PrintSquaresTester
{
public static void main (String[ ] args)
{ new PrintSquares(); //8
System.exit (0); //9
} //======================
}
import javax.swing.*;
public class View extends Object
{
private JTextArea area;
private JScrollPane scroller;
public View (int rows, int columns)
{ area = (rows < 1 || columns < 1) ? new JTextArea (2, 10)
: new JTextArea (rows, columns);
scroller = new JScrollPane (area);
} //======================
/** Show the message in a scrolled text area of a dialog. */
public void display (String message)
{ area.append (message); // no effect if message == null
IO.say (scroller);
} //======================
}
public class RepairOrder extends Object
{
private String itsFirstName = "";
private String itsLastName = "";
private String itsRepairJob = "";
private double itsEstimatedTime = 0.00;
public RepairOrder (String par)
{ if (par != null)
{ StringInfo si = new StringInfo (par);
si.trimFront (0); // remove any leading whitespace
this.itsFirstName = si.firstWord();
si.trimFront (this.itsFirstName.length());
this.itsLastName = si.firstWord();
si.trimFront (this.itsLastName.length());
this.itsRepairJob = si.toString();
}
} //======================
public String getClient()
{ return itsFirstName + " " + itsLastName;
} //======================
public String getRepairJob()
{ return itsRepairJob;
} //======================
public double getEstimatedTime()
{ return itsEstimatedTime;
} //======================
public void setEstimatedTime (double time)
{ if (time > 0.0 && time < 20.0)
itsEstimatedTime = time;
} //======================
public String toString()
{ return itsLastName + ", " + itsFirstName + ": "
+ itsRepairJob + ", time= " + itsEstimatedTime;
} //======================
}
/** To compile this, compile first the Queue class in Chapter 7 listings */
public class RepairShop
{
/** Repeatedly get repair jobs from the foreman or display
* the next repair job to the foreman, until there are no
* more jobs to be done. */
private static final String ENTER
= "Enter first name, last name, and description of job";
private static final String TIMEPROMPT
= "Estimate the time to complete the repair job";
private static final String CHOICES = "Enter A to add another"
+ " job, X to exit, anything else to process a job";
//////////////////////////////////////////////////
private View output = new View (40, 25);
private Queue jobQueue = new Queue();
private RepairOrder nextJob = new RepairOrder
(IO.askLine (ENTER));
private double totalTime = IO.askDouble (TIMEPROMPT);
public RepairShop()
{ nextJob.setEstimatedTime (totalTime); //1
jobQueue.enqueue (nextJob); //2
} //======================
public void processRequests()
{ String input = IO.askLine (CHOICES).toUpperCase(); //3
while ( ! input.equals ("X")) //4
{ if (input.equals ("A")) //5
{ nextJob = new RepairOrder (IO.askLine (ENTER)); //6
nextJob.setEstimatedTime (IO.askDouble (TIMEPROMPT));
jobQueue.enqueue (nextJob); //8
totalTime += nextJob.getEstimatedTime(); //9
output.display ("\nHours remaining: " + totalTime);
} //11
else if (jobQueue.isEmpty()) //12
IO.say ("No jobs currently in the queue!"); //13
else //14
{ nextJob = (RepairOrder) jobQueue.dequeue(); //15
totalTime -= nextJob.getEstimatedTime(); //16
output.display ("\nNext job: " + nextJob.toString()
+ "\nHours remaining: " + totalTime);
} //19
input = IO.askLine (CHOICES).toUpperCase(); //20
} //21
} //======================
} //########################################################
class RepairShopApp
{
public static void main (String[] args)
{ new RepairShop().processRequests(); //22
System.exit (0); //23
} //======================
}
|
Layout and Design Copyright © Psumonix, LLC. All Rights Reserved. |