|
CSCI A201/A597 and I210
|
To make sure you see these in time, we post them without paying any attention to formatting.
::::::::::::::
One.java
::::::::::::::
import java.io.*;
public class One {
public static void main(String[] args) {
double a, b, c;
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Please enter the value of a then press Enter : ");
a = console.readDouble();
System.out.print("Please enter the value of b then press Enter : ");
b = console.readDouble();
System.out.print("Please enter the value of c then press Enter : ");
c = console.readDouble();
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!
}
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Two.java
::::::::::::::
import java.io.*;
public class Two {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
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.");
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Three.java
::::::::::::::
import java.io.*;
public class Three {
public static void main(String[] args) {
double a, b, c;
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Please enter the value of a then press Enter : ");
a = console.readDouble();
System.out.print("Please enter the value of b then press Enter : ");
b = console.readDouble();
System.out.print("Please enter the value of c then press Enter : ");
c = console.readDouble();
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);
}
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Four.java
::::::::::::::
public class Four {
public static void main(String[] args) {
Circle circle;
ConsoleReader console;
circle = new Circle(110, 120, 100);
System.out.println("Welcome. Circle created. ");
System.out.println("Center is at: " + circle.getCenter());
System.out.println("Radius is: " + circle.getRadius());
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);
return distance <= radius * radius;
}
public String getCenter() {
return "(" + xCenter + ", " + yCenter + ")";
}
public double getRadius() {
return radius;
}
}
::::::::::::::
Five.java
::::::::::::::
public class Five {
public static void main(String[] args) {
ConsoleReader console;
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);
return distance <= radius * radius;
}
public String getCenter() {
return "(" + xCenter + ", " + yCenter + ")";
}
public double getRadius() {
return radius;
}
}
::::::::::::::
Six.java
::::::::::::::
import java.io.*;
public class Six {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Do you want to continue? ");
String answer = console.readLine();
if (answer.equalsIgnoreCase("Y") ||
answer.equalsIgnoreCase("Yes") ||
answer.equalsIgnoreCase("OK") ||
answer.equalsIgnoreCase("Sure") ||
answer.equalsIgnoreCase("Why not?"))
{
System.out.println("OK");
} else if (answer.equalsIgnoreCase("N") ||
answer.equalsIgnoreCase("No"))
{
System.out.println("Terminating");
} else
{
System.out.println("Bad input");
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Seven.java
::::::::::::::
import java.io.*;
public class Seven {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Enter a letter grade: ");
String grade = console.readLine();
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.out.println("Bad input.");
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.out.println("Bad input.");
System.exit(0);
}
if (value > 0 && value < 4.0) {
value += extra;
}
}
System.out.println("The numeric value is: " + value);
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Eight.java
::::::::::::::
import java.io.*;
public class Eight {
public static void main(String[] args) {
double score;
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Enter numeric score then press Enter : ");
score = console.readDouble();
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("");
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Nine.java
::::::::::::::
import java.io.*;
public class Nine {
public static void main(String[] args) {
String a, b, c;
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Enter three strings: ");
a = console.readLine();
b = console.readLine();
c = console.readLine();
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);
}
}
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Ten.java
::::::::::::::
import java.io.*;
public class Ten {
public static void main(String[] args) {
int year;
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Please enter the year then press Enter : ");
year = console.readInt();
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!");
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Eleven.java
::::::::::::::
import java.io.*;
public class Eleven {
public static void main(String[] args) {
ConsoleReader console = new ConsoleReader(System.in);
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 {
System.out.println("Bad input");
}
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
Twelve.java
::::::::::::::
import java.io.*;
public class Twelve {
public static void main(String[] args) {
double a, b;
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Comparing floating-point numbers.");
System.out.print("Enter the first number : ");
a = console.readDouble();
System.out.print("Enter the second number: ");
b = console.readDouble();
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.");
}
System.out.println("Thanks for asking.");
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
::::::::::::::
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;
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
class Thirteen {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
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: ");
b.deposit(c.readDouble());
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: ");
b.withdraw(c.readDouble());
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;
ConsoleReader console = new ConsoleReader(System.in);
System.out.print("Please enter employee's name then press Enter : ");
name = console.readLine();
System.out.print("Please enter hourly wage then press Enter : ");
wage = console.readDouble();
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);
}
}
class ConsoleReader {
public ConsoleReader(InputStream inStream) {
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
public String readLine() {
String inputLine = "";
try {
inputLine = reader.readLine();
} 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;
}
private BufferedReader reader;
}
Remember that there's always more than one way to achieve a result, and if
you have a different solution that's actually better!