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.

by java programming

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.



public class Fraction3YourName {
    private int sign;
    private int num;
    private int denom;

    public Fraction3YourName() {
        sign =1;
        denom =1;
    }

    public Fraction3YourName(int n) {
        sign =1;
        num = n;
        denom =1;
    }

    public Fraction3YourName(int s, int n, int d) {
        sign = s;
        num = n;
        denom = d;
    }
    // add the fraction
    public Fraction3YourName add(Fraction3YourName frac)
    {
        int denom = this.denom * frac.denom;
        int num = (this.num * frac.denom) + (frac.num*this.denom);
        int s=1;
        if(num<0)
            s=-1;
        return new Fraction3YourName(s,num,denom);
    }
    // subtract the fraction
    public Fraction3YourName subtract(Fraction3YourName frac)
    {
        int denom = this.denom * frac.denom;
        int num = (this.num * frac.denom) - (frac.num*this.denom);
        int s=1;
        if(num<0)
            s=-1;
        return new Fraction3YourName(s,num,denom);
    }
    // Multiply the fraction
    public Fraction3YourName multiply(Fraction3YourName frac)
    {
        int denom = this.denom * frac.denom;
        int num = (this.num * frac.num);
        int s=1;
        if(num<0)
            s=-1;
        return new Fraction3YourName(s,num,denom);
    }
    // Divide the fraction
    public Fraction3YourName divide(Fraction3YourName frac)
    {
        int denom = this.denom * frac.num;
        int num = (this.num * frac.denom);
        int s=1;
        if(num<0)
            s=-1;
        return new Fraction3YourName(s,num,denom);
    }
    // print the fraction number
    public void print()
    {
        System.out.println(num+"/"+denom);
    }

    public static void main(String[] args) {
        // create a fraction class using constructor 3
        Fraction3YourName f1=new Fraction3YourName(1,2,3);
        // print the fraction
        System.out.print("F1 is: ");
        f1.print();

        // create a fraction class using constructor 2
        Fraction3YourName f2=new Fraction3YourName(3);
        // print the fraction
        System.out.print("F2 is: ");
        f2.print();

        // add the fractions and print the result
        Fraction3YourName addedFraction= f1.add(f2);
        System.out.print("F1+F2 is: ");
        addedFraction.print();

        // subtract the fractions and print the result
        Fraction3YourName subtractedFraction= f1.subtract(f2);
        System.out.print("F1-F2 is: ");
        subtractedFraction.print();

        // multiply the fractions and print the result
        Fraction3YourName multipliedFraction= f1.multiply(f2);
        System.out.print("F1*F2 is: ");
        multipliedFraction.print();

        // divide the fractions and print the result
        Fraction3YourName dividedFraction= f1.divide(f2);
        System.out.print("F1/F2 is: ");
        dividedFraction.print();
    }
}

==================

SCREENSHOTS:

OUTPUT:

======================

F1 is: 2/3
F2 is: 3/1
F1+F2 is: 11/3
F1-F2 is: -7/3
F1*F2 is: 6/3
F1/F2 is: 2/9

=======================


Related Solutions

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...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
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”...
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
For a supermarket, define a Inventory class. All the Inventory objects will contain S,No, name of...
For a supermarket, define a Inventory class. All the Inventory objects will contain S,No, name of clerk preparing the inventory, each item with id, quantity available , minimum order quantity ,price and date of expiry. There is an array to describe the above details. Your program generate the stock based on the items quantity is equal to minimum order quantity or less than minimum order quantity and also display items to check date of expiry with current date.
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance:...
In c++, define a class with the name BankAccount and the following members: Data Members: accountBalance: balance held in the account interestRate: annual interest rate. accountID: unique 3 digit account number assigned to each BankAccount object. Use a static data member to generate this unique account number for each BankAccount count: A static data member to track the count of the number of BankAccount objects created. Member Functions void withdraw(double amount): function which withdraws an amount from accountBalance void deposit(double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT