CS 212 M4 Assignments 1. For the TimeType class in the Lecture Notes, write the laterThan method (definition seeing below) and add it to the TimeType class. Also, write a driver to read two time...

1 answer below »

CS 212 M4 Assignments






1. For the
TimeType
class in the Lecture Notes, write the
laterThan
method (definition seeing below) and add it to the
TimeType
class. Also, write a driver to read two time objects from the keyboard, and compare them by involving the
equals
or
laterThan
methods, and then output the corresponding messages to the monitor. For example, if the two times are the same, display the message, "times 1 and 2 are the same". If time1 is later than time2, then display "time 1 is later than time 2", etc.





public boolean laterThan (TimeType t)



{


/* if this object is later than the t object, meaning: this.hrs is greater than t.hrs; or their hrs’ are the same, and this.mins is greater than t.mins; or if their hrs and mins are the same, and this.secs is greater than t.secs


*/





}



Hint: refer to Example #3.




2. Based on the
Fraction
class in the Lecture Notes, write a driver class to read two fraction numbers from the keyboard, perform four basic arithmetic operations (+, -, *, /), and the output the results (in the format of 3/4 + 2/3 = 17/12, etc. in a column) to the monitor. Using a loop, so the process could run continuously.
Requirement: the fraction numbers should be read in the form of a fraction, for example, 1/2, 3/4, etc.





Hint: refer to Example #6.






3. Based on the
Fraction
class in the Lecture Notes, write a driver class to read two fraction numbers from the keyboard, calculate their average, and the output the result (must be fraction number) to the monitor. Using a loop, so the process could run continuously.
Requirement: the fraction numbers should be read in the form of a fraction, for example, 1/2, 3/4, etc.





Hint: refer to Example #8.










Answered Same DayJul 31, 2021

Answer To: CS 212 M4 Assignments 1. For the TimeType class in the Lecture Notes, write the laterThan method...

Pulkit answered on Jul 31 2021
162 Votes
m4assignments-lolbm5vl.docx
CS 212 M4 Assignments
1. For the TimeType class in the Lecture Notes, write the laterThan method (definition seeing below) and add it to the TimeType class. Also, write a driver to read two time objects from the keyboard, and compare them by involving the equals or laterThan methods, and then output the corresponding messages to the monitor. For example, if the two times are the same, display the message, "times 1 and 2 are the same". If time1 is later than time2, then display "time 1 is later than time 2", etc.
    public boolean laterThan (TimeType t)
    {
/* if this object is later than the t object, meaning: this.hrs is greater than t.hrs; or their hrs’ are the same, and this.mins is greater than t.mins; or if their hrs and mins are the same, and this.secs is greater than t.secs
*/
    }
Hint: refer to Example #3.
2. Based on the Fraction class in the Lecture Notes, write a driver class to read two fraction numbers from the keyboard, perform four basic arithmetic operations (+, -, *, /), and the output the results (in the format of 3/4 + 2/3 = 17/12, etc. in a column) to the monitor. Using a loop, so the process could run continuously. Requirement: the fraction numbers should be read in the form of a fraction, for example, 1/2, 3/4, etc.
Hint: refer to Example #6.
3. Based on the Fraction class in the Lecture Notes, write a driver class to read two fraction numbers from the keyboard, calculate their average, and t
he output the result (must be fraction number) to the monitor. Using a loop, so the process could run continuously. Requirement: the fraction numbers should be read in the form of a fraction, for example, 1/2, 3/4, etc.
Hint: refer to Example #8.
Page 1 of 1
Page 2 of 1
__MACOSX/._m4assignments-lolbm5vl.docx
m4code.docx
Module 4 Code Examples
Example 1
Write a user-defined class, Student, which includes two instance variables, name and 3-elements array scores, as well as instance methods, such as, set, avg, grade and writeOutput. Write the driver code to read two students’ information including name and 3 scores for each one, and then output that each student’s information including name, average, and grade (P or F) to the monitor by invoking the writeOutput method.
Note: assume that two students’ information is as follows. The newline character ‘\n’ is marked in the end of each line.
Joe Smith\n
90 80 87\n
April Ann Lee\n
78 84 91\n
Since input.nextLine() is used to read the second student’s full name, and it will not escape spaces and the newline, a dummy is needed to remove the highlighted ‘\n’ that follows the last score of the first student.
import java.util.Scanner;
public class Code4_1
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Student one = new Student();
        //one.writeOutput();
        System.out.println();
        for(int i=0; i<2; i++)
        {
            String name = input.nextLine();
            int t1 = input.nextInt();
            int t2 = input.nextInt();
            int t3 = input.nextInt();
            one.set(name, t1, t2, t3);
            one.writeOut();
            System.out.println();
            if(input.hasNextLine())
                input.nextLine(); // dummy to consume ‘\n’
        }
    }
}
//---------------------
import java.text.DecimalFormat;
public class Student
{
    DecimalFormat f = new DecimalFormat("0.0");
    private    String name;
    private int[] scores =new int[3]; // create the array
    public void set(String n, int s1, int s2, int s3)
    {
        name=n;
        scores[0]=s1;
        scores[1]=s2;
        scores[2]=s3;
    }
    public double avg()
    {
        double t=(scores[0]+scores[1]+scores[2])/3.0;
        return t;
    }
    public char grade()
    {
        if(avg()>=60)
            return 'P';
        else
            return 'F';
    }
    public void writeOut()
    {
        System.out.println("Name: " + name);
        System.out.println("Avg: " + f.format(avg()));
        System.out.println("Grade: " + grade());
    }
}
Note: there are no constructors declared in Student class, and Java will create a default one (with all zeros or nulls for instance variables) automatically.
Here, we use Intellij to run the program (Since a Student class is already existed in the old projects folder, we have to create a new folder for the programs in Module 4).
First, the Student class should be compiled, and the screen shot is shown as follows.
Then the driver class is executed that is shown as below.

Example 2
Based on the last Student class, write the betterThan method that returns true if the student’s average is higher than the one passed in the argument, and then add it to the Student class. Also, add the equals method to the class that returns true if its average is equal to the other. Then write a driver to read two students from the keyboard, and compare them, and then output the results to the monitor (such as "student 1 and 2 are the same", "student 1 is better than student 2", etc.).
The two new methods are added in the Student class and highlighted.
import java.util.Scanner;
public class Code4_2
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Student one = new Student();
        Student two = new Student();
        
        System.out.print("Stuent1 (name & 3 scores): ");
        String name = input.nextLine();
        int t1 = input.nextInt();
        int t2 = input.nextInt();
        int t3 = input.nextInt();
        one.set(name, t1, t2, t3);
        input.nextLine(); // dummy
            
        System.out.print("Stuent2 (name & 3 scores): ");
        name = input.nextLine();
        t1 = input.nextInt();
        t2 = input.nextInt();
        t3 = input.nextInt();
        two.set(name, t1, t2, t3);
        if(one.equals(two))
            System.out.print("\nThe two are the same.");
        else if(one.betterThan(two))
            System.out.print("\nStu1 is better than Stu2.");
        else
            System.out.print("\nStu2 is better than Stu1.");
    }
}
//---------------------
import java.text.DecimalFormat;
public class Student
{
    DecimalFormat f = new DecimalFormat("0.0");
    private    String name;
    private int[] scores =new int[3];
    public void set(String n, int s1, int s2, int s3)
    {
        name=n;
        scores[0]=s1;
        scores[1]=s2;
        scores[2]=s3;
    }
    public double avg()
    {
        double t=(scores[0]+scores[1]+scores[2])/3.0;
        return t;
    }
    public char grade()
    {
        if(avg()>=60)
            return 'P';
        else
            return 'F';
    }
    public void writeOut()
    {
        System.out.println("Name: " + name);
        System.out.println("Avg: " + f.format(avg()));
        System.out.println("Grade: " + grade());
    }
    public boolean betterThan(Student s1)
    {    return avg() > s1.avg();    }
    public boolean equals(Student s1)
    {    return avg() == s1.avg();    }
}
Compile the modified Student class first and the screen shot is shown below.
Then execute the driver class that is shown below.

Example 3
Based on the TimeType class in the Lecture Notes, write a driver to read two time objects from the keyboard, and compare them if they are same or not by invoking the equals method, and then output the corresponding messages to the monitor (such as "Time 1 and 2 are the same", or "The two are different"). Using a loop, so the code could be run continuously.
import java.util.Scanner;
public class Code4_3
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        TimeType one = new TimeType();
        TimeType ano = new TimeType();
        boolean go = true;
        String ans;
        while(go)
        {
            System.out.print("Time1: (h m s): ");
            int h = input.nextInt();
            int m = input.nextInt();
            int s = input.nextInt();
            one.set(h, m, s);
            System.out.print("Time2: (h m s): ");
            h = input.nextInt();
            m = input.nextInt();
            s = input.nextInt();
            ano.set(h, m, s);
            if(one.equals(ano))
                System.out.print("\nThe two are the same.");
            else
                System.out.print("\nThey are different.");
            System.out.print("\nQuit(y/n): ");
            ans = input.next();
            if(ans.equalsIgnoreCase("y"))
                go = false;
        }
    }
}
//------------------------------
public class TimeType
{
    private int hrs;
    private int mins;
    private int secs;
    public void set(int h, int m, int s)
    {
        hrs = h;
        mins = m;
        secs = s;
    }
    public void writeOut()
    {
        if (hrs < 10)
            System.out.print("0");
        System.out.print(hrs + ":");
        if (mins < 10)
            System.out.print("0");
        System.out.print(mins + ":");
        if ( secs < 10 )
            System.out.print("0");
        System.out.print(secs);
    }
    public void increment ()
    {
        secs ++;
        if (secs > 59)
        {
            secs = 0;
            mins ++;
            if (mins > 59)
            {
                mins = 0;
                hrs ++;
                if (hrs > 23)
                    hrs = 0;
            }
        }
    }
    public boolean equals (TimeType t)
    {
/* if this object is the same as the t object, return true;
    otherwise, return false
*/
        if (this.hrs==t.hrs&&this.mins==t.mins&&thissecs==t.secs)
            return true;
        else
            return false;
/* Alternative, converting hrs and mins into value in seconds, and then compare them
    return (hrs*2600+mins*60+secs==t.hrs*3600+t.mins*60+t.secs);
*/
    }
}
Screen shots of the program in TextPad and execution results are as follows.

Example 4
Based on the Fraction class in the Lecture Notes, write a driver class to calculate the fraction operation 1/2 + 3/4. The two operands should be declared as objects of Fraction class.
class Code4_4
{
    public static void main(String[] args)
    {
        Fraction x = new Fraction(1, 2);
        Fraction y = new Fraction(3, 4);
        Fraction z = x.add(y);
        x.writeOut();
        System.out.print(" + ");
        y.writeOut();
        System.out.print(" = ");
        z.writeOut();
        System.out.println("\n");
    }
}
//------------------------
public class Fraction
{
    private int nume,deno;
    private void reduce() // reduce to lowest form
    {
        int n = nume;
        int d = deno;
        while(d != 0)
        {
            int r = n % d;
            n = d;
            d = r;
        }
        if (n != 0)
        {
            nume /= n;
            deno /= n;
        }
        if (deno < 0)
        {
            nume *= (-1);
            deno *= (-1);
        }
    }
    public Fraction()
    {
        nume = 0;
        deno = 1;
    }
    public Fraction (int n, int d)
    {
        nume = n;
        deno = d;
    }
    public void set (int n, int d)
    {
        nume = n;
        deno = d;
    }
    public Fraction add(Fraction x) // this + x ==> add
    {
        Fraction temp = new Fraction(this.nume*x.deno+this.deno*x.nume,this.deno*x.deno);
        temp.reduce();
        return temp;
    }
    public Fraction sub(Fraction x) // this - x ==> sub
    {
        Fraction temp = new Fraction(this.nume*x.deno-this.deno*x.nume,this.deno*x.deno);
        temp.reduce();
        return temp;
    }
    public Fraction mult(Fraction x)
    {
        Fraction temp = new Fraction(nume*x.nume, deno*x.deno);
        temp.reduce();
        return temp;
    }
    public Fraction div(Fraction x)
    {
        Fraction temp = new Fraction(nume*x.deno, deno*x.nume);
        temp.reduce();
        return temp;
    }
     public void writeOut()
    {
        reduce();
        System.out.print(nume + "/" + deno);
    }
}    
We use IntelliJ for this program.
First, compile the Fraction class and the screen shot is as blow.
Then, run the driver class and the screen shot is as below.
Example 5
Based on the Fraction class in the Lecture Notes, write a driver that prompts messages and lets the user input two fraction numbers, perform the + and – operations, and then output the fraction number results to the monitor.
import java.util.Scanner;
class Code4_5
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Input x.nume: ");
        int xn = input.nextInt();
        System.out.print("Input x.deno: ");
        int xd = input.nextInt();
        Fraction x = new Fraction(xn, xd);
        System.out.print("Input y.nume: ");
        int yn = input.nextInt();
        System.out.print("Input y.deno: ");
        int yd = input.nextInt();
        Fraction y = new Fraction(yn, yd);
        Fraction z = x.add(y);
        System.out.println("\n\nThe answers are: \n");
        x.writeOut();
        System.out.print(" + ");
        y.writeOut();
        System.out.print(" = ");
        z.writeOut();
        System.out.println("\n--------------");
        z = x.sub(y);
        x.writeOut();
        System.out.print(" - ");
        y.writeOut();
        System.out.print(" = ");
        z.writeOut();
        System.out.println("\n");
    }
}
(Fraction class same as the last example)
Screen shots of the program in TextPad and execution results are as follows.
Example 6
Based on the Fraction class in the Lecture Notes, write a driver that prompts messages and lets the user input two fraction numbers, perform the + and – operations, and then output the fraction number results to the monitor. Requirement: the fraction numbers should be read in the form of a fraction, for example, 1/2, 3/4, etc.
Note: to read the fraction number as a whole, please refer to the aFrac method in Lecture Note, P.13. The Scanner object, input, should be declared as a global static variable used for both the main and aFrac methods.
import java.util.Scanner;
class Code4_6
{
static Scanner input=new Scanner(System.in);
public static void main(String[] args)
{
Fraction x, y, z;
String answer;
while(true)
{
System.out.print("Input 1st fraction: ");
x = aFrac();
System.out.print("Input 2nd fraction: ");
y = aFrac();
z = x.add(y);
System.out.println("The answers are: ");
x.writeOut();
System.out.print(" + ");
y.writeOut();
System.out.print(" = ");
z.writeOut();
System.out.println("\n--------------");
z = x.sub(y);
x.writeOut();
System.out.print(" - ");
y.writeOut();
System.out.print(" = ");
z.writeOut();
System.out.println("\nContinue (y/n)? ");
answer = input.next();
if(answer.equalsIgnoreCase("n")) break;
}
System.out.println("\n");
}
//-------------------------------
private static Fraction aFrac()
{
String str = input.next();
String[] parts = str.split("/", 2);
int n = Integer.parseInt(parts[0]);
int d = Integer.parseInt(parts[1]);
Fraction frac = new Fraction(n, d);
return frac;
}
}
(Fraction class same as the example #4)
Screen shots of the program in IntelliJ and execution results are as follows.
Example 7
Based on the Fraction class in the Lecture Notes, write a driver that prompts messages and lets the user input a fraction number, an operator of +, -, *, or /, another fraction number, and then the code performs the corresponding operation for the two fraction numbers, and then output the fraction number results to the monitor. Requirement: the fraction numbers should be read in the form of a fraction, for example, 1/2,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here