Here's what I got so far. I am trying to add the two fractions together while following the UML. I attached the assignment to this as well.
I have no Idea how to create the add method. It's for an online class, and this one is just killing me.
Thank You
import java.util.Scanner; // Needed for the Scanner class
public class Program4Demo
{
public static void main(String[] args)
{
//Create an object in the scanner class
Scanner keyboard = new Scanner(System.in);
//Declare object reference variable
Program4 number1;
Program4 number2;
Program4 number3;
//Get and store number 1
System.out.println("Enter the numerator and denominator for number 1: ");
number1 = new Program4(keyboard.nextInt(), keyboard.nextInt());
//Get and store number 2
System.out.println("Enter the numerator and denominator for number 2: ");
number2 = new Program4(keyboard.nextInt(), keyboard.nextInt());
//Display numbers
System.out.println("Number 1 is: " + number1);
System.out.println("Number 2 is: " + number2);
System.out.println("");
//Display addition
number3 = new Program4(number1.add(number2));
System.out.println("Addition is: " + ????);
}
}
public class Program4
{
//Create fields
private int numerator;
private int denominator;
//Constructor for fraction
public Program4(int n, int d)
{
numerator = n;
denominator = d;
}
//Setter and getter for numerator
public void setNumerator(int n)
{
numerator = n;
}
public int getNumerator()
{
return numerator;
}
//Setter and getter for denominator
public void setDenominator(int d)
{
denominator = d;
}
public int getDenominator()
{
return denominator;
}
//Setter for fraction
public void setFraction(int n, int d)
{
numerator = n;
denominator = d;
}
//Getter for fraction as double
public double getAsDouble()
{
return (double) numerator / (double) denominator;
}
//toString Method
public String toString()
{
String str = numerator + "/" + denominator;
return str;
}
//Addition method
public Program4 add(Program4 number2)
{
numerator = (number1.getNumerator * number2.getDenominator) + (number1.getDenominator
* number2.getNumerator());
denominator = number1.getDenominator() * number2.getDenominator());
return ????;
}
}