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 ????;
	 }
}

number3 = new Program4(number1.add(number2));

What is the signature for the add() method? What args does it take and what does it return?

Program4 is a weird name for a class. Shouldn't it be something like Fraction?

You should post the program assignment directly and not require a pdf download.

What is the signature for the add() method? What args does it take and what does it return?

Program4 is a weird name for a class. Shouldn't it be something like Fraction?

You should post the program assignment directly and not require a pdf download.

i'm not sure what you mean by the signature for the add method, but it is supposed to take arguments of the Fraction class and return arguments of the Fraction class as well.

I named the class Program4 because I already have a file called fraction from a previous assignment that I still have to submit.

I'll try copying and pasting the assignment into the original post.

Here is the assignment:

Program 4
Learning Objectives
· Demonstrate the ability to write a simple Java project
that includes a user-defined class: Fraction
· Demonstrate how to use instance variables and
methods in your user-defined class
· Demonstrate the use of objects in your driver program

Program 4 Assignment
Re-do your Fraction class from Lab 4 by adding four more
instance methods: add, subtract, multiply, and divide.
Methods add, subtract, multiply, and divide should accept a
Fraction argument passed as the parameter and return a
Fraction.
In your FractionDemo main method, read in the numerator and
demoninator for two Fraction objects from the user, display
their values as fractions (using toString), and then display the
results of the four arithmetic methods.
Fraction Arithmetic
In case you have forgotten your fraction arithmetic, here is a
quick crib sheet for addition, subtraction, multiplication, and
division. Do not worry about reducing your fractions
(e.g. 4 / 8 is fine, do not reduce it to 1 / 2)

Test Case:
Enter the numerator and denominator for number1: 3 4
Enter the numerator and denominator for number2: 2 3
number1 is: 3 / 4
number2 is: 2 / 3
Addition: 17 / 12
Subtraction: 1 / 12
Multiplication: 6 / 12
Division: 9 / 8

UML:
Fraction
- numerator : int
- denominator : int
+ Fraction (n: int, d: int)
+ setNumerator (n: int) : void
+ getNumerator ( ) : int
+ setDenominator (d: int) : void
+ getDenominator ( ) : int
+ setFraction (n: int, d:int) : void
+ getAsDouble ( ) : double
+ toString ( ) : String
+ add (f2: Fraction): Fraction
+ subtract (f2: Fraction): Fraction
+ multiply (f2: Fraction): Fraction
+ divide (f2: Fraction): Fraction

The signature for a method states what it returns and what its args are:

public int add(int i){}

returns an int and takes an int as argument.

How do you define your add() method?

Change the class name to something with the word Fraction in it. Program4 makes no sense.

The signature for a method states what it returns and what its args are:

public int add(int i){}

returns an int and takes an int as argument.

How do you define your add() method?

Change the class name to something with the word Fraction in it. Program4 makes no sense.

the add method is supposed to take in a Fraction arg and return a Fraction.

I was going to change the name of the class back to fraction once I submitted my other assignment title fraction, which is due by midnight tonight.

You've got 90% of the add() method coded. You need to clean up the call to it and create the Fraction object its supposed to return.

Do you want the add() method to be mutator or not? If so, you can keep what you do but change the way you create your method in your main. Assuming that all your current codes are working.

Add method...

//Addition method
//I use 'this' to be more explicit in calling its own variable.
public void add(Program4 number2) {
  this.numerator = (this.numerator * number2.getDenominator()) + (this.denominator * number2.getNumerator());
  this.denominator = this.denominator * number2.getDenominator();
}

Main...

// change these lines...
// number3 = new Program4(number1.add(number2));
// System.out.println("Addition is: " + ????);
// to...
number1.add(number2);
number3 = new Program4(number1.getNominator(), number1.getDenominator());
System.out.println("Addition is: " + number3);

If not, then you need to create a new Program4 instance inside your add() and should not mutate your current number.

//Addition method
//I use 'this' to be more explicit in calling its own variable.
public void add(Program4 number2) {
  int numer = (this.numerator * number2.getDenominator()) + (this.denominator * number2.getNumerator());
  int denom = this.denominator * number2.getDenominator();
  Program4 retObj = new Program4(number, denom);
}

//in Main
number3 = (number1.add(number2));
System.out.println("Addition is: " + number3);

When you are returning something, you may need to create it inside method and return it. In your case, you want Program4 object to be returned, then you need to create it inside your add(). You could create mutator methods but that would be a bit confusing to you right now. But remember that you need to be clear whether you want your method to be producer (create and return without changing the current state of the caller object) or mutator (state could be changed).

Hey,
If you don't want to modify your existing class it would be better for you to extend your existing Fraction class as follows:

FractionWithOperations extends Fraction{
 public Fraction add(Fraction fr){
  newNumerator=numerator*fr.getDenominator()+denominator*fr.getNumerator();
  newDenominator=denominator*fr.getDenominator();
  return new Fraction(newNumerator, newDenominator);
 }
 public Fraction subtract(Fraction fr){
  newNumerator=numerator*fr.getDenominator()-denominator*fr.getNumerator();
  newDenominator=denominator*fr.getDenominator();
  return new Fraction(newNumerator, newDenominator);
 }
 public Fraction multiply(Fraction fr){
  newNumerator=numerator*fr.getNumerator();
  newDenominator=denominator*fr.getDenominator();
  return new Fraction(newNumerator, newDenominator);
 }
 public Fraction divide(Fraction fr){
  newNumerator=numerator*fr.getDenominator;
  newDenominator=denominator*fr.getDenominator();
  return new Fraction(newNumerator, newNumertor);
 }
}

------------------------------------------------------------------------------------------------------------------------------
1. Create a class called Fraction. The Fraction class should define a “new data type” such that it will be used
as the data type for a variable that will hold a typical fraction. A typical fraction is composed of a
numerator and a denominator.
2. Create a class (program called FractionArithmetic. The Fraction Arithmetic class should be an executable
program that will allow the user to compute the sum, difference, product and quotient of two fractions.
When FractionArithmetic is executed, the user will be shown a menu from which he/she can choose to add,
subtract, multiply or divide two fractions. The numerators and denominators of the fractions that will serve
as operands are entered at run time by the user. The result (sum, difference, product or quotient) should be
displayed in the typical fraction form.
Below is an incomplete Fraction class
------------------------------------------------------------------------------------------------------
/**
*The Fraction class
*A template for a fraction that has the form numerator/denominator
*/
public class Fraction {
// data members
private int numerator; // holds the numerator of this fraction
private int denominator; // holds the denominator of this fraction
/**
*Constructs a fraction with numerator=0 and denominator = 1
*This constructor allows a Fraction with an equivalent numeric value of zero to
*be created… This becomes the default constructor (since it has no arguments)…
* Example of usage: Fraction f = new Fraction();
**/
public Fraction() {
numerator = 0;
denominator = 1;
}
/**
* This constructor creates a Fraction with the specified whole number as its initial value…
* Example of usage: Fraction f = new Fraction(10);
*/
public Fraction(int wholeNumVal) {
this.numerator = wholeNumVal;
denominator = 1;
}
/*
Alternative version of the constructor above, implemented by calling the
constructor defined below, using the this reference variable…
public Fraction(int wholeNumVal) {
this(wholeNumVal, 1);
}
*/
/**
*This constructor creates a Fraction using the explicit numerator and denominator values…
*Example of usage: Fraction f = new Fraction(5, 2);
*/
public Fraction(int numerator, int denominator) {
this.numerator = numerator; // the keyword this may be omitted
this.denominator = denominator; // the keyword this may be omittted
}
/**
*Accessor/Getter Method
*Returns the value of the numerator of this fraction
**/
public int getNumerator(){
return numerator;
}
/**
*Accessor/Getter Method
*Returns the value of the denominator of this fraction
**/
public int getDenominator(){
return denominator;
}
/**
*Mutator/Setter Method
*Sets the value of the numerator of this fraction to n
**/
public void setNumerator(int n){
numerator=n;
}
/**
*Mutator/Setter Method
*Sets the value of the denominator of this fraction to d
**/
public void setDenominator(int d){
denominator=d;
}
/**
*Returns a string form of the fraction following the format numerator/denominator
**/
public String toString(){
return (numerator + "/" + denominator );
}
/**
*Returns the reduced(simpliest) form of this fraction
**/
public Fraction reduce(){
Fraction r= new Fraction(); // construct a fraction
int gCF = computeGCF(); // determine the greatest common factor of numerator and denominator
int newN= numerator/gCF; //compute newN, the numerator of the simpliest form of this fraction
//compute newD, the denominator of the simpliest form of this fraction
int newD = denominator/gCF;
r.setNumerator(newN); // set the numerator of the simpliest form to newN
r.setDenominator(newD);// set the denominator of the simpliest form to newD
return r; // return the simpliest form of this fraction
}
// Compuets the greastest common factor of the numerator and denominator
private int computeGCF(){
int gCF=1;
int lesser = 1;
boolean found=false;
lesser = computeLesser(numerator, denominator);
for (int candidate= lesser; (candidate >= 1 && !found); candidate--){
if (numerator%candidate == 0 && denominator%candidate==0){
found = true;
gCF= candidate;
}
}
return gCF;
}
//Returns the lesser integer between n1 and n2
private int computeLesser(int n1, int n2){
int lesser=n1;
if (n1<n2)
lesser = n1;
else
lesser = n2;
return lesser;
}
/**
*Returns the sum of this fraction and another fraction g
**/
public Fraction sum(Fraction g){
Fraction s=new Fraction();
int den = denominator * g.getDenominator();
int num = den/denominator*numerator + den/g.getDenominator()*g.getNumerator();
s.setNumerator(num);
s.setDenominator(den);
return s.reduce();
}
} // end of Fraction class
------------------------------------------------------------------------------------------------------------------------
Checklist of Implied /Additional Requirements
1. The methods already included may be adopted/improved
2. The Fraction class should have a subtract method
3. The Fraction class should have a multiply method
4. The Fraction class should have a divide method
5. The FractionArithmetic class should allow a continuous program execution until the users chooses to quit.
Sample run:
This program helps you perform arithmetic operations with fractions.
What do you want to do?
1. Add fractions
2. Subtract fractions
3. Multiply fractions
4. Divide fractions
5. Reduce a fraction
6. Quit
Enter your choice<1/2/3/4/5/6>: 1
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 2
Enter the numerator of the second fraction: 5
Enter the denominator of the second fraction: 4
Sum = 7/4
Press enter to continue…
This program helps you perform arithmetic operations with fractions.
What do you want to do?
1. Add fractions
2. Subtract fractions
3. Multiply fractions
4. Divide fractions
5. Reduce a fraction
6. Quit
Enter your choice<1/2/3/4/5/6>: 3
Enter the numerator of the first fraction: 1
Enter the denominator of the first fraction: 2
Enter the numerator of the second fraction: 3
Enter the denominator of the second fraction: 5
Product = 3/10
Press enter to continue…
This program helps you perform arithmetic operations with fractions.
What do you want to do?
1. Add fractions
2. Subtract fractions
3. Multiply fractions
4. Divide fractions
5. Reduce a fraction
6. Quit
Enter your choice<1/2/3/4/5/6>: 5
Enter the numerator of the fraction: 9
Enter the denominator of the fraction: 27
Reduced form = 1/3
This program helps you perform arithmetic operations with fractions.
What do you want to do?
1. Add fractions
2. Subtract fractions
3. Multiply fractions
4. Divide fractions
5. Reduce a fraction
6. Quit
Enter your choice<1/2/3/4/5/6>: 7
Invalid choice! Please choose 1, 2, 3, 4, 5 or 6 only.
Enter your choice<1/2/3/4/5/6>: 6
Thank you for using the program.
6. The toString method should be improved such that if the denominator of the fraction is 1, the denominator
needs not be shown. ( i.e. 2/1 should be displayed as 2). In addition, if the numerator is 0, the fraction
should be presented as 0. (i.e. 0/10 is displayed as 0.)

can you help me please..thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.