Question

In: Computer Science

Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class *...

Define a class Fraction3YourName as follows,

/**
* Program Name: Fraction3YourName.java
* Discussion: Fraction3Yourname class
* written By:
* Date: 2019/09/19
*/
public class Fraction3YourName {
private int sign;
private int num;
private int denom;
public Fraction3YourName() {
//sign = ;
//denom = ;
}
public Fraction3YourName(int n) {
//sign = ;
//num = n;
//denom = ;
}
public Fraction3YourName(int s, int n, int d) {
//sign = s;
//num = n;
//denom = d;
}
}
You are asked to
 Complete the definitions for the given constructors; and
 Provide additional method members to allow the performance of four simple
arithmetic operations: (1) Addition, (2) Subtraction, (3) Multiplication, and (4)
Division; and
 Provide one method member print() to display the Fraction3YourName object.

/** REQUIRED PROGRAM OUTPUT

****************************************
* MENU – HW #3 *
* (1) Creating 2 Fraction3 Objects *
* (2) Performing Arithmetic Operations *
* (3) Displaying Fraction3 Objects *
* (4) Quit *
****************************************
Enter your option (1 through 4): 5
You are funny!

****************************************
* MENU – HW #3 *
* (1) Creating 2 Fraction3 Objects *
* (2) Performing Arithmetic Operations *
* (3) Displaying Fraction3 Objects *
* (4) Quit *
****************************************
Enter your option (1 through 4): 2
Creating 2 Fraction3 Objects –
// Your details

****************************************
* MENU – HW #3 *
* (1) Creating 2 Fraction3 Objects *
* (2) Performing Arithmetic Operations *
* (3) Displaying Fraction3 Objects *
* (4) Quit *
****************************************
Enter your option (1 through 4): 3
Displaying Fraction3 Objects –
// Your details

****************************************
* MENU – HW #3 *
* (1) Creating 2 Fraction3 Objects *
* (2) Performing Arithmetic Operations *
* (3) Displaying Fraction3 Objects *
* (4) Quit *
****************************************
Enter your option (1 through 4): 4
Performing Arithmetic Operations –
// Your details

****************************************
* MENU – HW #3 *
* (1) Creating 2 Fraction3 Objects *
* (2) Performing Arithmetic Operations *
* (3) Displaying Fraction3 Objects *
* (4) Quit *
****************************************
Enter your option (1 through 4): 4
Having fun!

by java programming

Solutions

Expert Solution

Please find the solution and rate the answer.

import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);

    static Fractions getFraction() {

        System.out.println("enter sign as - or +");
        int sign = sc.next().charAt(0) == '-' ? -1 : 1;
        System.out.println("Enter numerator");
        int num = sc.nextInt();
        System.out.println("Enter denominator");
        int denom = sc.nextInt();
        return new Fractions(sign, num, denom);

    }

    public static void main(String[] args) {
        boolean loop = true;
        do {
            System.out.println("Enter fraction object 1");
            Fractions one = getFraction();
            System.out.println("Etner frac 2");
            Fractions two = getFraction();
            System.out.println("Multply(m),Divide(d),subtract(s),add(a),Exit(e)");
            char choice;

            choice = sc.next().charAt(0);
            if (choice == 'e') break;
            ;
            switch (choice) {
                case 'm':
                    one.multiply(two);
                    System.out.println(one);
                    break;
                case 'd':
                    one.divide(two);
                    System.out.println(one);
                    break;
                case 'a':
                    one.add(two);
                    System.out.println(one);
                    break;
                case 's':
                    one.subtract(two);
                    System.out.println(one);
                    break;
            }

        } while (loop);

//
//        System.out.println(fr);
    }
}
public class Fractions {
    private int sign;
    private int num;
    private int denom;

    public Fractions() {
        num = 1;
        denom = 1;
    }

    public Fractions(int n) {
        setSign(n);
        num = n;
    }

    public Fractions(int s, int n, int d) {
        setSign(s);
        num = n;
        denom = d;
    }

    @Override
    public String toString() {
        return ""+(num*sign)+"/"+denom+"";
    }

    public int getSign() {
        return sign;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getDenom() {
        return denom;
    }

    public void setDenom(int denom) {
        this.denom = denom;
    }

    private void setSign(int n) {
        if (n < 0) {
            sign = -1;
        } else {
            sign = +1;
        }
    }

    public void add(Fractions fr) {

        num = (this.num * fr.getDenom() * sign) + (fr.getNum() * denom * fr.getSign());
        if(num<0){
            sign=-1;
        }else{
            sign=1;
        }
        num=Math.abs(num);
        denom = Math.abs(fr.getDenom() * denom);
        normalize();
//        sign = fr.getSign() * sign;
    }
    public void subtract(Fractions fr){
        fr.setSign(-1*fr.getSign());
        add(fr);
        normalize();
    }
    public void multiply(Fractions fr) {
        sign = fr.getSign() * sign;
        num = Math.abs(fr.getNum() * num);
        denom = Math.abs(fr.getDenom()*denom);
        normalize();
    }

    private void normalize() {
        int gcd = gcd(num, denom);
        num = num/ gcd;
        denom = denom/gcd;
    }

    public void divide(Fractions fr) {
        sign = fr.getSign() * sign;
        num = Math.abs(num * fr.getDenom());
        denom = Math.abs(fr.getNum() * denom);
        normalize();
    }
    int gcd(int one, int two)
    {
        if (one == 0)
            return two;
        if (two == 0)
            return one;

        if (one == two)
            return one;

        if (one > two)
            return gcd(one-two, two);
        return gcd(one, two-one);
    }

}

Sample out:


Related Solutions

Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class *...
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class * written By: * Date: 2019/09/19 */ public class Fraction3YourName { private int sign; private int num; private int denom; public Fraction3YourName() { //sign = ; //denom = ; } public Fraction3YourName(int n) { //sign = ; //num = n; //denom = ; } public Fraction3YourName(int s, int n, int d) { //sign = s; //num = n; //denom = d; } } You are...
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class *...
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class * written By: * Date: 2019/09/19 */ public class Fraction3YourName { private int sign; private int num; private int denom; public Fraction3YourName() { //sign = ; //denom = ; } public Fraction3YourName(int n) { //sign = ; //num = n; //denom = ; } public Fraction3YourName(int s, int n, int d) { //sign = s; //num = n; //denom = d; } } by java...
Define a JAVA class ISP. The class consists of the name of the subscription plan and...
Define a JAVA class ISP. The class consists of the name of the subscription plan and a method that display the plan. Derive a class DPlan from ISP. The DPlan will charge RM10 per Mbps subscribe and RM0.20 per GB used. Derive a class MPlan from ISP. The MPlan will charge RM5 per Mbps subscribe and RM0.80 per GB used. Display the plan and select the best plan.
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions: 1. print to print out the alarm’s description and hour, minute, and second of the alarm time. 2. setDescription to accept a string argument and use it to set the description member variable. 3. setAtime to accept three integers (for hour, minute, and...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private...
A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class Alarm also includes the following public member functions: 1. print to print out the alarm’s description and hour, minute, and second of the alarm time. 2. setDescription to accept a string argument and use it to set the description member variable. 3. setAtime to accept three integers (for hour, minute, and...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
Question1: Define a class Human that contains: - The data fields "first name" and "last name"...
Question1: Define a class Human that contains: - The data fields "first name" and "last name" - A constructor that sets the first and last name to the instance variables. Create also a no-argument constructor - A getName() method which prints the first and last name Define the class Student which inherits Human and contains: - Private data field "mark" of type integer and a constructor which calls the superclass constructor Define the class Worker which inherits Human and contains:...
Goals Understand class structure and encapsulation Description Define a class Product to hold the name and...
Goals Understand class structure and encapsulation Description Define a class Product to hold the name and price of items in a grocery store. Encapsulate the fields and provide getters and setters. Create an application for a grocery store to calculate the total bill for each customer. Such program will read a list of products purchased and the quantity of each item. Each line in the bill consists of: ProductName ProductPrice Quantity/Weight The list of items will be terminated by “end”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT