Spring Semester 2002

Iffy Practice Problems: This is textbook chapter 5. Programming problems. The Solutions.

The solutions presented below are in `plain` format.

```::::::::::::::
One.java
::::::::::::::

import java.io.*;

public class One {
public static void main(String[] args) {
double a, b, c;
System.out.print("Please enter the value of a then press Enter : ");
System.out.print("Please enter the value of b then press Enter : ");
System.out.print("Please enter the value of c then press Enter : ");
double x1, x2;
if (b * b - 4 * a * c < 0) {
System.out.println("There are no solutions.");
} else {
if (a == 0) {
if (b == 0) {
if (c == 0) {
System.out.println("Identity: zero == zero.");
} else {
System.out.println(c + " is not zero");
}
} else {
System.out.println(-c/b);
}
} else {

// Notice a big mistake here:

x1 = - b - Math.sqrt(b * b - 4 * a * c) / (2 * a);
// should be:          x1 = (- b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);

x2 = - b + Math.sqrt(b * b - 4 * a * c) / (2 * a);
// should be:          x2 = (- b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);

System.out.println(x1 + " " + x2);

// Thanks to nminibay for catching the error (also see text page 75)

// Apologies for the unexplainable lack of attention. Darn!

}
}
}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Two.java
::::::::::::::

import java.io.*;

public class Two {
public static void main(String[] args) {

System.out.print("Enter the card notation: ");
String card = console.readLine();

String one = card.substring(0, 1);
String two = card.substring(1, 2);

if      (one.equalsIgnoreCase("A"))  System.out.print("Ace of ");
else if (one.equals("2"))            System.out.print("Two of ");
else if (one.equals("3"))            System.out.print("Three of ");
else if (one.equals("4"))            System.out.print("Four of ");
else if (one.equals("5"))            System.out.print("Five of ");
else if (one.equals("6"))            System.out.print("Six of ");
else if (one.equals("7"))            System.out.print("Seven of ");
else if (one.equals("8"))            System.out.print("Eight of ");
else if (one.equals("9"))            System.out.print("Nine of ");
else if (one.equals("1"))  {
if (two.equals("0")) {
System.out.print("Ten of ");
two = card.substring(2, 3);
}
}
else if (one.equalsIgnoreCase("J"))  System.out.print("Jack of ");
else if (one.equalsIgnoreCase("Q"))  System.out.print("Queen of ");
else if (one.equalsIgnoreCase("K"))  System.out.print("King of ");
else System.out.print("Unknown denomination of ");

if      (two.equalsIgnoreCase("D")) System.out.println("diamonds.");
else if (two.equalsIgnoreCase("H")) System.out.println("hearts.");
else if (two.equalsIgnoreCase("S")) System.out.println("spades.");
else if (two.equalsIgnoreCase("C")) System.out.println("clubs.");
else System.out.println("unknown colour.");

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Three.java
::::::::::::::

import java.io.*;

public class Three {
public static void main(String[] args) {
double a, b, c;
System.out.print("Please enter the value of a then press Enter : ");
System.out.print("Please enter the value of b then press Enter : ");
System.out.print("Please enter the value of c then press Enter : ");

if (a >= c) {
if (a >= b) {
System.out.println("The largest number is: " + a);
} else {
System.out.println("The largest number is: " + b);
}
} else {
if (c >= b) {
System.out.println("The largest number is: " + c);
} else {
System.out.println("The largest number is: " + b);
}
}

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Four.java
::::::::::::::

public class Four {
public static void main(String[] args) {
Circle circle;
circle = new Circle(110, 120, 100);
System.out.println("Welcome. Circle created. ");
System.out.println("Center is at: " + circle.getCenter());
System.out.println("You will be asked to specify a point.");
System.out.println("First enter x, then enter y.");
System.out.print("Please enter x now: ");
console = new ConsoleReader(System.in);
double x = console.readDouble();
System.out.print("Please enter y now: ");
double y = console.readDouble();
if (circle.contains(x, y)) {
System.out.println("Congratulations.");
} else {
System.out.println("You missed.");
}
}
}

class Circle {
private double xCenter, yCenter, radius;
Circle(double x, double y, double r) {
xCenter = x; yCenter = y; radius = r;
}
public boolean contains(double xPoint, double yPoint) {
double distance = Math.pow(xCenter - xPoint, 2) +
Math.pow(yCenter - yPoint, 2);
}
public String getCenter() {
return "(" + xCenter + ", " + yCenter + ")";
}
public double getRadius() {
}
}

::::::::::::::
Five.java
::::::::::::::

public class Five {
public static void main(String[] args) {
console = new ConsoleReader(System.in);
System.out.print("Welcome. Please specify the " +
"radius of the first circle: ");
double r1 = console.readDouble();
System.out.print("Great. Now please specify the " +
"radius of the second circle: ");
double r2 = console.readDouble();
Circle combined = new Circle(100, 200, r1 + r2);
if (combined.contains(200, 100)) {
System.out.println("Circles intersect.");
} else {
System.out.println("Circles don't intersect.");
}
}
}

class Circle {
private double xCenter, yCenter, radius;
Circle(double x, double y, double r) {
xCenter = x; yCenter = y; radius = r;
}
public boolean contains(double xPoint, double yPoint) {
double distance = Math.pow(xCenter - xPoint, 2) +
Math.pow(yCenter - yPoint, 2);
}
public String getCenter() {
return "(" + xCenter + ", " + yCenter + ")";
}
public double getRadius() {
}
}

::::::::::::::
Six.java
::::::::::::::

import java.io.*;

public class Six {
public static void main(String[] args) {

System.out.print("Do you want to continue? ");

{
System.out.println("OK");

} else if (answer.equalsIgnoreCase("N") ||
{
System.out.println("Terminating");

} else
{
}

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Seven.java
::::::::::::::

import java.io.*;

public class Seven {
public static void main(String[] args) {

System.out.print("Enter a letter grade: ");

String letter = grade.substring(0, 1);

double value = 0;

if (letter.equals("A")) {
value = 4;
} else if (letter.equals("B")) {
value = 3;
} else if (letter.equals("C")) {
value = 2;
} else if (letter.equals("D")) {
value = 1;
} else if (letter.equals("F")) {
value = 0;
} else {
System.exit(0);
}

if (grade.length() > 1) {
String sign = grade.substring(1, 2);

double extra = 0;

if (sign.equals("+")) {
extra = 0.3;
} else if (sign.equals("-")) {
extra = -0.3;
} else {
System.exit(0);
}

if (value > 0 && value < 4.0) {
value += extra;
}

}

System.out.println("The numeric value is: " + value);

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Eight.java
::::::::::::::

import java.io.*;

public class Eight {
public static void main(String[] args) {

double score;

System.out.print("Enter numeric score then press Enter : ");

if        (score >= 4.0) {
System.out.println("A+");

} else if (score >= 3.85) {
System.out.println("A");

} else if (score >= 3.5) {
System.out.println("A-");

} else if (score >= 3.15) {
System.out.println("B+");

} else if (score >= 2.85) {
System.out.println("B");

} else if (score >= 2.5) {
System.out.println("B-");

} else if (score >= 2.15) {
System.out.println("C+");

} else if (score >= 1.85) {
System.out.println("C");

} else if (score >= 1.5) {
System.out.println("C-");

} else if (score >= 1.15) {
System.out.println("D+");

} else if (score >= 0.85) {
System.out.println("D");

} else if (score >= 0.35) {
System.out.println("D-");

} else {
System.out.println("");

}

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Nine.java
::::::::::::::

import java.io.*;

public class Nine {
public static void main(String[] args) {

String a, b, c;

System.out.println("Enter three strings: ");

if (a.compareTo(b) < 0) {                                   // a, b
if (a.compareTo(c) < 0) {                               // a, c
if (b.compareTo(c) < 0) {                           // a, b, c
System.out.println(a + "\n" + b + "\n" + c);
} else {                                            // a, c, b
System.out.println(a + "\n" + c + "\n" + b);
}
} else {                                                // c, a, b
System.out.println(c + "\n" + a + "\n" + b);
}
} else {                                                    // b, a
if (a.compareTo(c) < 0) {                               // b, a, c
System.out.println(b + "\n" + a + "\n" + c);
} else {                                                // c, a
if (b.compareTo(c) < 0) {                           // b, c, a
System.out.println(b + "\n" + c + "\n" + a);
} else {                                            // c, b, a
System.out.println(c + "\n" + b + "\n" + a);
}
}
}
}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Ten.java
::::::::::::::

import java.io.*;

public class Ten {
public static void main(String[] args) {

int year;

System.out.print("Please enter the year then press Enter : ");

if (    (((year % 4)  == 0) && ((year % 100) != 0) || (year < 1582))
||
(  year % 400 == 0)
) {

System.out.println("Leap year: " + year);

} else {

System.out.println(year + " not a leap year!");

}

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Eleven.java
::::::::::::::

import java.io.*;

public class Eleven {
public static void main(String[] args) {
System.out.print("Enter a month : ");
int m = console.readInt();

if        (m == 1) {
System.out.println("31 days");
} else if (m == 2) {
System.out.println("28 or 29 days");
} else if (m == 3) {
System.out.println("31 days");
} else if (m == 4) {
System.out.println("30 days");
} else if (m == 5) {
System.out.println("31 days");
} else if (m == 6) {
System.out.println("30 days");
} else if (m == 7) {
System.out.println("31 days");
} else if (m == 8) {
System.out.println("31 days");
} else if (m == 9) {
System.out.println("30 days");
} else if (m == 10) {
System.out.println("31 days");
} else if (m == 11) {
System.out.println("30 days");
} else if (m == 12) {
System.out.println("31 days");
} else {
}

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Twelve.java
::::::::::::::

import java.io.*;

public class Twelve {
public static void main(String[] args) {

double a, b;

System.out.println("Comparing floating-point numbers.");

System.out.print("Enter the first number : ");
System.out.print("Enter the second number: ");

int n, m;

n = (int)(100 * a);
m = (int)(100 * b);

if (n == m)
{
System.out.println(a + " and " + b +
" are the same up to two decimal places.");
} else
{
System.out.println(a + " and " + b +
" are NOT the same up to two decimal places.");
}

if (Math.abs(a - b) < 0.01)
{
System.out.println(a + " and " + b +
" are within 0.01 of one another.");
} else
{
System.out.println(a + " and " + b +
" are NOT within 0.01 of one another.");
}

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

::::::::::::::
Thirteen.java
::::::::::::::

import java.io.*;

class BankAccount {

double balance;

BankAccount() {

}

BankAccount(double initialBalance) {
this.balance = initialBalance;
}

void withdraw(double amount) {

if (this.balance > amount) {
this.balance = this.balance - amount;
} else {
System.out.println("Sorry, you cannot do that.");
}

}

void deposit(double amount) {
if (amount > 0) {
this.balance = this.balance + amount;
} else {
System.out.println("Sorry, you cannot do that.");
}

}

double getBalance() {
return balance;
}

}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
}

class Thirteen {
public static void main(String[] args) {
System.out.println("Hello, and welcome to JavaOne Bank.");
System.out.println("An account will be created for you.");
System.out.println("What will the initial balance be?");
System.out.println("Type it now: ");
BankAccount b = new BankAccount(c.readDouble());
System.out.println("The current balance in your account is: "
+ b.getBalance());
System.out.println("You now want to make a deposit. How much?");
System.out.println("Type the amount here: ");
System.out.println("The current balance in your account is: "
+ b.getBalance());
System.out.println("You now want to make a withdrawal. How much?");
System.out.println("Type it now: ");
System.out.println("The current balance in your account is: "
+ b.getBalance());
System.out.println("Thanks for using class BankAccount. Good-bye!");
}
}

::::::::::::::
Fourteen.java
::::::::::::::

import java.io.*;

public class Fourteen {
public static void main(String[] args) {

String name;
double wage;

System.out.print("Please enter employee's name then press Enter : ");

System.out.print("Please enter hourly wage then press Enter : ");

System.out.print("Please enter hours worked then press Enter: ");

double hours = console.readDouble();

double overtime = (hours - 40);

System.out.println(" Paycheck for employee "  + name  + "\n\n" +
" Hours worked: " + hours + "\n" +
" Hourly wage: "  + wage  + "\n"   );

double pay;

if (overtime > 0 ) {

pay = 40 * wage + overtime * (1.5 *wage);

System.out.println(
" Overtime hours: " + overtime + "\n" +
" Overtime hourly wage: " + (1.5 * wage) + "\n");

} else {

pay = hours * wage;

}

System.out.println(" Total payment: " + pay);

}
}

public ConsoleReader(InputStream inStream) {
inStream));
}
public String readLine() {
String inputLine = "";
try {
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public int readInt() {
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
public double readDouble() {
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}