Practice 1

  1. Read Chapter 1,2
  2. Write a program to find out answer(s) on page 41 questions 5.
  3. Write a program to find out answer(s) on page 41 questions 7.
  4. Write a method named format to accept floating point and decimal places and return floating point. It can accept double or float types. Use Math.pow(x,y) or Math.round(x). e.g. 103 returned by Math(10,3); 357 returned by Math.round(356.55678) ;
  5. Write a test program to test the format method.
The answers or solutions are for your reference

Chapter 1 section A: 1.) a. Legal b. Illegal c. Illegal d. Legal e. Legal f. Illegal g. Legal h. Illegal i. Legal j. Illegal k. Legal l. Legal

2.) a. screen size, model, serial number b. amount, issue date, check number c. insurance ID, name, phone, physician

//7.
// displays learning objectives
class Objectives{
   public static void main(String[] args){
       System.out.print("About programming tasks"
                        + "\nObject-oriented programming concepts"
                        + "\nAbout the Java programming language"
                        + "\nHow to write a Java program"
                        + "\nHow to add comments to a Java program"
                        + "\nHow to run a Java program"
                        + "\nHow to modify a Java program");
    }
}
Chapter 1 Section B 2. a. T f. F b. T g. T c. T h. F d. F i. T e. T j. F

3. a. 2.76 b. 3.67 c. 5.0 d. 1.0 e. 0.0

//5.
class Room {
    public static void main(String[] args) {
        float length, width;
        length = 15;
        width = 25;
        System.out.println("The floor space is "
                    + length * width
                    + " square feet\n");
    }
}
//6
class Carpet {
    public static void main(String[] args){
        double length, width, price;
        length = 15;
        width = 25;
        price = 5.25;
        System.out.println("The floor space is "
                + length * width
                + " square feet");
        System.out.println("The price for carpet is $"
                + (length * width) * price);
    }
}

//7. 
// displays the floor space of a room and price of
// carpeting in square yards
class Yards {
    public static void main(String[] args){
        float length, width, price;
        length = 25.0;
        width = 42.0;
        price = 8.5;
        System.out.println("The floor space is "
                + (length * width) / 9
                + " square yards");
        System.out.println("The price for carpet is $"
                + ((length * width) /9) * price);
    }
}

// 8.
// converts minutes to hours and minutes
class Time {
    public static void main(String[] args){
        int minutes = 197;
        int hours = 197 / 60;
        int mins = 197 % 60;
        System.out.println(minutes + " minutes becomes "
                + hours + " hours and "
                + mins + " minutes");
    }
}

// 9.
// displays initials
class Initials {
    public static void main(String[] args){
        char init1 = 'M';
        char init2 = 'G';
        char init3 = 'S';
        System.out.println(init1 + "."
                + init2 + "."
                + init3 + ".");
    }
}

// 10.
// displays tuition and book fees
class Fees {
    public static void main(String[] args){
        double tuitionFee = 650.00;
        double bookFee = 75.00;
        System.out.println("Total fees: $"
                    + (tuitionFee + bookFee));
    }
}

//11. 
// displays wage and payroll information
class Payroll {
    public static void main(String[] args) {
        double hourlyRate = 40;
        double hoursWorked = 42;
        double grossPay, tax, netPay;
        grossPay = hourlyRate * hoursWorked;
        tax = grossPay * .15;
        netPay = grossPay - tax;
        System.out.println("Gross pay: $"
                    + grossPay
                    + "\nWithholding tax: $" + tax
                    + "\nNet pay: $" + netPay);
    }
}

// 12.
// displays conversion into currency denominations
class Dollars {
    public static void main(String[] args){
        int dollarAmount = 57;
        calcChange(dollarAmount);
    }
    
    public static void calcChange(int dollars) {
        int twenties, tens, fives, ones, amount;
        twenties = dollars / 20;
        amount = dollars - (twenties * 20);
        tens = amount / 10;
        amount = amount - (tens * 10);
        fives = amount / 5;
        amount = amount - (fives * 5);
        ones = amount / 1;
        System.out.println("$" + dollars + " converted is "
                    + twenties + " $20, "
                    + tens + " $10, "
                    + fives + " $5, and "
                    + ones + " $1");
    }
}

//13. 
// calculates simple interest
class Interest {
     public static void main(String[] args){
        double investment = 1000;
        double rate = .05;
        System.out.println("The interest for $"
                + investment + " invested for one year at "
                + rate + " is: "
                + calcInterest(investment, rate));
    }

    public static double calcInterest(double amount, double rate) {
        return amount * rate;
    }
}

//14. 
// calculates weekly salary
class Salary {
    public static void main(String[] args) {
        double straightHours = 40.0;
        double overtimeHours = 13.0;
        double hourlyRate = 25.0;
        System.out.println("The weekly salary for "
                + straightHours + " regular hours and "
                + overtimeHours + " \novertime hours at "
                + hourlyRate + " per hour is: $"
                + calcSalary(straightHours, overtimeHours,
                hourlyRate));
    }
    
    public static double calcSalary(double reg, double ot,
                                    double rate){
        return (reg * rate) + (ot * (1.5 * rate));
    }
}
//Chapter 2 p63.
//2.
public class Numbers {
    public static void main (String args[]) {
        int num1 = 8, num2 = 3;
        sum(num1, num2);
        difference(num1, num2);
        System.out.println("The product is: " + product(num1,num2));
    }
    
    public static void sum(int n1, int n2) {
        System.out.println("The sum is " + (n1 + n2));
    }
    
    public static void difference(int n1, int n2) {
        System.out.println("The difference is " + (n1 - n2));
    }
    
    public static int product(int n1, int n2) {
        return n1 * n2;
    }
}

//3. 
// uses a method to calculate dozens of eggs
public class Eggs {
    public static void main (String args[])    {
        int numberOfEggs = 153;
        calcDozens(numberOfEggs);
    }
    
    public static void calcDozens(int numEggs) {
        int dozens, extras;
        dozens = numEggs / 12;
        extras = numEggs - (dozens * 12);
        System.out.println(numEggs + " eggs results in " + dozens
                + " dozen eggs with " + extras + " left over.");
    }
}

//4. 
// uses a method to display initials two different ways
public class Monogram {
    public static void main (String args[]) {
        char init1 = 'M', init2 = 'G', init3 = 'S';
        showInitials(init1, init2, init3);
    }
    
    public static void showInitials(char a, char b, char c) {
        System.out.println("First, middle, last: "
                + a + "." + b + "." + c + ".");
        System.out.println("First, last, middle: "
                + a + "." + c + "." + b + ".");
    }
}

//5. 
// uses two methods to square and cube an int
public class Exponent {
    public static void main (String args[]) {
        int theNumber = 5;
        System.out.println(theNumber + " squared is "
                + square(theNumber) + "\n"
                + theNumber + " cubed is "
                + cube(theNumber));
    }
    
    public static int square(int x) {
        return x * x;
    }
    
    public static int cube(int x) {
        return x * x * x;
    }
}

//6.
// cubes a number in a method
public class Cube {
    public static void main (String args[]) {
        int theNumber = 5;
        System.out.println(theNumber + " cubed is "
                + cubeIt(theNumber));
    }
    
    public static int cubeIt(int x) {
        return x * x * x;
    }
}

// 7.
// contains two classes with method to calculate the
// result of a sales transaction
class Calc {
    public static double sale(double price, double comm, 
                              double disc) {
        return (price + comm) - disc;
    }
}
public class Calculator {
    public static void main (String args[]) {
        double price = 500.00;
        double commission = price * .05;
        double discount = price * .10;
        System.out.println("The result for a price of "
                + price
                + " with a commission of "
                + commission
                + "\nand a discount of "
                + discount
                + " is "
                + Calc.sale(price, commission, discount));
    }
}

//8. 
// calls a method in the Cube class to cube a number
class Divide {
    public static void main (String args[]){
        int num1 = 50, num2 = 3;
        divide(num1, num2);
    }
    
    public static void divide(int x, int y) {
        System.out.println("The quotient of "
                + x + " and " + y + " is "
                + (x / y));
        System.out.println("The remainder of "
                + x + " / " + y + " is "
                + (x % y));
    }
}


//1a. Pizza.java
// Chapter 2, Section B, Exercise 1a
// creates a class to store info about a pizza
class Pizza {
    // the private data members
    private int diameter;
    private double price;
    private String toppings;
    // the public get and set methods
    public void setDiameter(int amt){
                diameter = amt;
        }
        public int getDiameter(){
                return diameter;
        }
        public void setPrice(double amt){
                price = amt;
        }
        public double getPrice(){
                return price;
        }
        public void setToppings(String str){
                toppings = str;
        }
        public String getToppings(){
                return toppings;
        }
}

//1b. TestPizza.java
// Chapter 2, Section B, Exercise 1b
// client to test the Pizza class
class TestPizza        {
        public static void main (String[] args){
                Pizza pie = new Pizza();
                pie.setDiameter(15);
                pie.setPrice(8.95);
                pie.setToppings("Mushroom, Pepperoni");
                System.out.println("You have ordered a pizza with "
                                + pie.getToppings()
                                + " toppings,\nwith a diameter of "
                                + pie.getDiameter()
                                + " and a price of "
                                + pie.getPrice());
        }
}

//2a and 2c. Student.java
// Chapter 2, Section B, Exercise 2
// creates a class to store info about a student
class Student {
        // the private data members
        private int IDnumber;
        private int hours;
        private int points;
        // constructor added in exercise 2c
        Student(){
                IDnumber = 9999;
                points = 12;
                hours = 3;
        }
        // end of constructor
        // the public get and set methods
        public void setIDnumber(int number){
                IDnumber = number;
        }
        public int getIDnumber(){
                return IDnumber;
        }
        public void setHours(int number){
                hours = number;
        }
        public int getHours(){
                return hours;
        }
        public void setPoints(int number){
                points = number;
        }
        public int getPoints(){
                return points;
        }
        // methods to display the fields
        public void showIDnumber(){
                System.out.println("ID Number is: " + IDnumber);
        }
        public void showHours(){
                System.out.println("Credit Hours: " + hours);
        }
        public void showPoints(){
                System.out.println("Points Earned: " + points);
        }
        public double getGradePoint(){
                return (double) (points / hours);
        }
}

//2b. ShowStudent.java
// Chapter 2, Section B, Exercise 2b
// client to test the Student class
class ShowStudent{
    public static void main (String[] args){
        Student pupil = new Student();
        pupil.setIDnumber(1234);
        pupil.setPoints(16);
        pupil.setHours(4);
        System.out.println("The grade point average is "
                                + pupil.getGradePoint());
        pupil.showIDnumber();
        pupil.showPoints();
        pupil.showHours();
    }
}

//3a. Circle.java
// Chapter 2, Section B, Exercise 3a
// creates a class to store info about a circle
class Circle {
         // the private data members
        private double radius;
        private double area;
        private double diamter;
        Circle(){
                radius = 1;
        }
        public void setRadius(double r){
                radius = r;
        }
        public double getRadius(){
                return radius;
        }
        public double computeDiamter(){
                return radius * 2;
        }
        public double computeArea(){
                return ((radius * radius) * 3.14);
        }
}

//2b. ShowStudent.java
// Chapter 2, Section B, Exercise 2b
// client to test the Student class
class ShowStudent {
    public static void main (String[] args){
         Student pupil = new Student();
         pupil.setIDnumber(1234);
         pupil.setPoints(16);
         pupil.setHours(4);
         System.out.println("The grade point average is "
                    + pupil.getGradePoint());
         pupil.showIDnumber();
         pupil.showPoints();
         pupil.showHours();
    }
}


//4a. Checkup.java
// Chapter 2, Section B, Exercise 4a
// creates a class to store info about a patient
class Checkup {
        private int patientID;
        private int systolic;
        private int diastolic;
        private int LDL;
        private int HDL;
        public Checkup(int pid, int sys, int di, int ldl, int hdl){
                patientID = pid;
                systolic = sys;
                diastolic = di;
                LDL = ldl;
                HDL = hdl;
        }
        public void setPatientID(int num){
                patientID = num;
        }
        public void setSystolic(int num){
                systolic = num;
        }
        public void setDiastolic(int num){
                diastolic = num;
        }
        public void setLDL(int num){
                LDL = num;
        }
        public void setHDL(int num){
                HDL = num;
        }
        // get functions
        public int getPatientID(){
                return patientID;
        }
        public int getSystolic(){
                return systolic;
        }
        public int getDiastolic(){
                return diastolic;
        }
        public int getLDL(){
                return LDL;
        }
        public int getHDL(){
                return HDL;
        }
        public void computeRatio(){
                System.out.println("The LDL/HDL ratio is "
                + (float)(LDL/HDL));
        }
        public void explainRatio(){
                System.out.println(
                "Good cholesterol is an LDL/HDL ratio of 3.5 or lower"
                + "\nLDL is known as 'good cholesterol'");
        }
}

//4b. TestCheckup.java
// Chapter 2, Section B, Exercise 4b
// client to test the Checkup class
class TestCheckup
    public static void main (String[] args){
         Checkup patient1 = new Checkup(1,110,78,100,40);
         Checkup patient2 = new Checkup(2,160,90,180,140);
         Checkup patient3 = new Checkup(3,140,80,90,30);
         Checkup patient4 = new Checkup(4,100,70,120,60);
         showValues(patient1);
         showValues(patient2);
         showValues(patient3);
         showValues(patient4);
    }
    public static void showValues(Checkup a){
        System.out.println("Your checkup results:"
              + "\nID: " + a.getPatientID()
              + "\nSystolic: " + a.getSystolic()
              + "\nDiastolic: " + a.getDiastolic()
              + "\nLDL: " + a.getLDL()
              + "\nHDL: " + a.getHDL() );
         a.computeRatio();
    }
}

//5. Employee.java
// Chapter 2, Section B, Exercise 5
// Creates a class for employee info and a client class
class Emp {
        private int empNum;
        private String empLastName;
        private String empFirstName;
        public int getEmpNum() {
                return empNum;
        }
        public void setEmpNum(int num){
                empNum = num;
        }
        public String getFirstName(){
                return empFirstName;
        }
        public void setFirstName(String name){
                empFirstName = name;
        }
        public String getLastName(){
                return empLastName;
        }
        public void setLastName(String name){
                empLastName = name;
        }
}

public class Employee{
        public static void main(String[] args){
                Emp a = new Emp();
                Emp b = new Emp();
                a.setEmpNum(100);
                a.setFirstName("John");
                a.setLastName("Doe");
                b.setEmpNum(200);
                b.setFirstName("Tom");
                b.setLastName("Jones");
                System.out.println("ID\tFirst\tLast\n");
                System.out.println( a.getEmpNum()
                        + "\t" + a.getFirstName()
                        + "\t" + a.getLastName());
                System.out.println( b.getEmpNum()
                        + "\t" + b.getFirstName()
                        + "\t" + b.getLastName());
        }
}

//6. Invoice.java
// Chapter 2, Section B, Exercise 6
// Creates a class for an invoice and a client class
class Inv {
        private String itemName;
        private int quantity;
        private double price;
        public int getQuantity() {
                return quantity;
        }
        public void setQuantity(int num){
                quantity = num;
        }
        public double getPrice(){
                return price;
        }
        public void setPrice(double num){
                price = num;
        }
        public String getItemName(){
                return itemName;
        }
        public void setItemName(String name){
                itemName = name;
        }
        public double totCost(){
                return price * quantity;
        }
}
public class Invoice {
        public static void main(String[] args){
                Inv a = new Inv();
                Inv b = new Inv();
                a.setPrice(10.99);
                a.setQuantity(4);
                a.setItemName("hammer");
                b.setPrice(8.99);
                b.setQuantity(3);
                b.setItemName("shovel");
                System.out.println("Item\tPrice\tQuan\tTotal\n");
                System.out.println( a.getItemName()
                        + "\t" + a.getPrice()
                        + "\t" + a.getQuantity()
                        + "\t" + a.totCost());
                System.out.println( b.getItemName()
                        + "\t" + b.getPrice()
                        + "\t" + b.getQuantity()
                        + "\t" + b.totCost());
        }
}

//7. RoomSchedule.java
// Chapter 2, Section B, Exercise 7
// Creates a class for a meeting scheduler and a client class
class Room {
        private String meetingName;
        private String startTime;
        private String endTime;
        private String weekDay;
        public String getStartTime() {
                return startTime;
        }
        public void setStartTime(String time) {
                startTime = time;
        }
        public String getEndTime(){
                return endTime;
        }
        public void setEndTime(String time){
                endTime = time;
        }
        public String getMeetingName(){
                return meetingName;
        }
        public void setMeetingName(String name){
                meetingName = name;
        }
        public String getWeekDay(){
                return weekDay;
        }
        public void setWeekDay(String day){
                weekDay = day;
        }
}
public class RoomSchedule {
        public static void main(String[] args){
        Room a = new Room();
        Room b = new Room();
        a.setStartTime("10 AM");
        a.setEndTime("11:30 AM");
        a.setMeetingName("Finance Meeting");
        a.setWeekDay("MON");
        b.setStartTime("1:00 PM");
        b.setEndTime("2:30 PM");
        b.setMeetingName("Marketing Meeting");
        b.setWeekDay("WED");
        System.out.println( a.getMeetingName()
                + "\n" + a.getWeekDay()
                + "\t" + a.getStartTime()
                + "\t" + a.getEndTime());
        System.out.println( b.getMeetingName()
                + "\n" + b.getWeekDay()
                + "\t" + b.getStartTime()
                + "\t" + b.getEndTime());
        }
}