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...
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 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...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT