Question

In: Computer Science

Java The whole idea is for me to gain a better understanding of the logic. If...

Java

The whole idea is for me to gain a better understanding of the logic. If possible, can you go off of what I have started and not just give me a whole new written program?

Also, don't simplify result.

Implement a class named “Fraction” with the following properties:

  1. numerator: int type, private

  2. denominator: int type, private

and the following methods:

  1. one default constructor which will create a fraction of 1/1.

  2. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters.

  3. int getNum() : retrieves the value of numerator

  4. int getDenom(): retrieves the value of the denominator

  5. Fraction add(Fraction frac): adds with another Fraction number and returns the result in a new Fraction object.

  6. Fraction sub(Fraction frac): is subtracted by another Fraction frac and returns the result in a new Fraction object.

  7. Fraction mult(Fraction frac): multiplies with another Fraction number and returns the result in a new Fraction object.

  8. Faction div(Fraction frac): is divided by another Fraction number and returns the result in a new Fraction object.

  9. void print(): prints the Fraction number out

This is what I have so far, but I am getting hung up..

import java.util.Scanner;

public class Fraction {
private int numerator;
private int denominator;

//Default constructor that sets 1/1 fraction
Fraction(){
numerator=1;
denominator=1;
}

//Constructor that creates fraction with specified parameters
Fraction(int newNumerator, int newDenominator){
numerator = newNumerator;
denominator = newDenominator;
}

//Retreives value of numerator
public int getNum(){
return numerator;
}

//Retreives value of denominator
public int getDenom(){
return denominator;
}

//Adds fraction with another and returns value as Fraction object
public Fraction add(Fraction frac){
if(frac.denominator==denominator){
int num= frac.numerator+numerator;
int denom = denominator;
}else{
int denom = this.getDenom() * frac.getDenom();
int num = this.getNum()*frac.getDenom() + frac.getNum()*this.getDenom();
}
return new Fraction(num,denom);
}

//Subtracts fraction with another and returns value as Fraction object
public Fraction sub(Fraction frac){
int num = frac.numerator+numerator;
int denom= frac.denominator-denominator;
return new Fraction(num,denom);
  
}

//Multiplies fraction with another and returns value as Fraction object
public Fraction mult(Fraction frac){
int num = frac.getNum()*numerator;
int denom = frac.getDenom()*denominator;
return new Fraction(num,denom);
}

//Divides fraction with another and returns value as Fraction object
public Fraction div(Fraction frac){
int num = frac.getDenom()*numerator;
int denom = frac.getNum()*denominator;
return new Fraction(num,denom);
}

public void print(){
System.out.print(numerator + "/" + denominator);
}

Solutions

Expert Solution

import java.util.Scanner;

public class Fraction {
   private int numerator;
   private int denominator;

   //Default constructor that sets 1/1 fraction
   Fraction(){
      
       numerator=1;
       denominator=1;
   }

   //Constructor that creates fraction with specified parameters
   Fraction(int newNumerator, int newDenominator){
       // check that denominator is not 0, if it is 0 set it to 1
       if(newDenominator == 0)
           newDenominator = 1;
          
       numerator = newNumerator;
       denominator = newDenominator;
   }

   //Retreives value of numerator
   public int getNum(){
       return numerator;
   }

   //Retreives value of denominator
   public int getDenom(){
       return denominator;
   }

   //Adds fraction with another and returns value as Fraction object
   public Fraction add(Fraction frac){
      
       // no need to check equality of denominator, if denominator of both fractions is 1, then numerator = addition of both numerators
      
       int num = this.getNum()*frac.getDenom() + frac.getNum()*this.getDenom();
      
       int denom = this.getDenom() * frac.getDenom();
      
       return new Fraction(num,denom);
   }

   //Subtracts fraction with another and returns value as Fraction object
   public Fraction sub(Fraction frac){
      
       // numerator will be (product of numerator of first fraction and denominator of second fraction) - (product of denominator of first fraction and numerator of second fraction)
       int num = this.getNum()*frac.getDenom() - frac.getNum()*this.getDenom();
      
       int denom = this.getDenom() * frac.getDenom(); // denominator will be product of both denominators
      
      
       return new Fraction(num,denom);
  
   }

   //Multiplies fraction with another and returns value as Fraction object
   public Fraction mult(Fraction frac){
       int num = frac.getNum()*numerator;
       int denom = frac.getDenom()*denominator;
       return new Fraction(num,denom);
   }

   //Divides fraction with another and returns value as Fraction object
   public Fraction div(Fraction frac){
       int num = frac.getDenom()*numerator;
       int denom = frac.getNum()*denominator;
       return new Fraction(num,denom);
   }

   public void print(){
       System.out.print(numerator + "/" + denominator);
   }
}


Related Solutions

The purpose of this assignment is to gain a better understanding of the code sets used...
The purpose of this assignment is to gain a better understanding of the code sets used for medical billing. These sets can be complicated, but you will learn in the EHR#8 assignment this week, that the EHR practice management functions for billing can use the clinical information to help you with code selection. Coding Classification Sets for Medical Coding and Billing Code Set When is this Code Set used? Format Example Source CPT Category I CPT Category I codes are...
You are to select a local sports management facility to gain a better understanding on the...
You are to select a local sports management facility to gain a better understanding on the day-to-day facility management upkeep. You are to interview a facility or general manager to understand what goes into the upkeep of the facility including financial budget and time budget. You can interview someone at your local college, high school or gym.
You are to select a local sports management facility to gain a better understanding on the...
You are to select a local sports management facility to gain a better understanding on the day-to-day facility management upkeep. You are to interview a facility or general manager to understand what goes into the upkeep of the facility including financial budget and time budget. You can interview someone at your local college, high school or gym. You are to complete the Cleaning Table for Facility document. You should write a memo to the facility GM (to the instructor) detailing...
The owners, new in their role as healthcare entrepreneurs, wish to gain a better understanding of...
The owners, new in their role as healthcare entrepreneurs, wish to gain a better understanding of competition in the market and enlist you to prepare a associated assessment. Your task is to conduct the assessment using Porter's Five Forces Model. In doing so, consult a variety of information sources (e.g., telephone directories, the Internet, industry databases), your own firsthand knowledge of the marketplace, and other relevant sources in an effort to populate Porter's Five Forces framework. deliver real-world value, you...
Intel would like to gain a better understanding of how businesses select PC and network communications...
Intel would like to gain a better understanding of how businesses select PC and network communications products. What type of research design should be adopted?
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
Define and give examples for better understanding 1. What are all the data types of java...
Define and give examples for better understanding 1. What are all the data types of java script? 2. What are all the SQL joins? 3. What is polymorphism in your own words? 4. What is a constructor?
What is better whole life or term insurance?
What is better whole life or term insurance?
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic...
Overview and objective: Basic logic In this homework, you will exercise your understanding of boolean logic and the gates and circuits that embody it. A thorough understanding of boolean logic is extremely useful for almost all programming tasks and necessary to correctly implement complex systems. Additionally, greater familiarity with boolean logic should improve one’s ability to think critically in general as it forms the basis for all human reasoning. Technical Description and Instructions: 1. Consider the logical expression ( !x...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: Person. Define Class Person Write the class header Class Variables Class Person is to have the following data members: firstName of type String, lastName of type String representing a person’s first and last names and ssn of type int representing a social security number. Each data member has an access specifier of type private. Constructors Class Person...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT