Jump to content

parameter passing and creating methods

Featured Replies

i'm taking a computer science class in school and having a tough time understanding parameter passing and creating methods. i have this assignment i need to do that asks for the following:

 

The program first asks the user to enter three rational numbers (fractions).

Then the program finds the rational numbers that reperesent the maximum value and the minimum value and prints them (as rational numbers).

Finally it also prints the average of the three rational numbers as a rational number too.

For example, if the user enters 1/2, 2/5 and 3/8 the program must print the following:

 

Maximum: 1 / 2

Minimum: 3 / 8

Average: 17 / 40

 

ive made a bit of progress so far but am stuck on making methods to check for a min and max rational. i apologize for my sloppy code. i am new to this and any helpful suggestions will be appreciated :)

 

import java.util.Scanner;

 

class AddRationals {

 

public static void main (String[] args)

{

String div;

int a, b, c, d, e, f;

Scanner scan = new Scanner(System.in);

 

System.out.print("Type a rational number (a / b): ");

a = scan.nextInt();

div = scan.next("/");

b = scan.nextInt();

 

System.out.print("Type another rational number (c / d): ");

c = scan.nextInt();

div = scan.next("/");

d = scan.nextInt();

 

System.out.print("Type another rational number (e / f): ");

e = scan.nextInt();

div = scan.next("/");

f = scan.nextInt();

 

Rational p = new Rational(a,b); // p points to a new instance of Rational

Rational q = new Rational(c,d); // q points to a new instance of Rational

Rational s = new Rational(e,f); // s points to a new instance of Rational

 

Rational r = p.sum(q); // no need of new here, sum returns a new Ratonal object

Rational t = r.sum(s); //add rationals to find final sum of all three rationals

 

 

System.out.println(p + " + " + q + " = " + r);

r.reduce();

System.out.println("Reduced sum is: " + r);

 

if (decimal(p)>=decimal(q)&&decimal(p)>=decimal(s)) {Rational max = p;}

// if (q>=p&&q>=s) maximum=q;

// if (s>

 

System.out.println("Maximum: " + max);

System.out.println("Minimum: ");

 

//Rational two = new Rational(2,1);//divide by 3,1 or 3/1

Rational ave = r.divide(new Rational(3,1));//three

 

System.out.println("The average is " + ave);

}

 

}

 

class Rational {

 

private int numerator;

private int denominator;

 

public Rational (int numerator, int denominator)

{

this.numerator = numerator;

this.denominator = denominator;

}

 

public Rational sum (Rational r)

{

int num, den;

den = denominator * r.getDenominator();

num = numerator * r.getDenominator() + r.getNumerator() * denominator;

return new Rational (num, den);

}

 

 

public Rational divide (Rational r)

{

int num, den;

num = numerator*r.getDenominator();

den = denominator*r.getNumerator();

return new Rational (num, den);

}

 

public double Decimal (Rational r)

{

double decimal;

decimal = numerator/denominator;

return decimal; //a*d>=b*c

}

 

 

public int getNumerator() { return numerator; }

 

public int getDenominator() { return denominator; }

 

public String toString () { return numerator + "/" + denominator; }

 

public void reduce() {

 

int factor;

factor = gcd(numerator, denominator);

numerator = numerator / factor;

denominator = denominator / factor;

}

 

private int gcd (int x, int y) {

 

int t;

while (y>0) {

t = x % y;

x = y;

y = t;

}

return x;

}

 

}

LOL, just realized I need to learn programing one day, I will try to google it. Might as well start now at least start the foundation.

Edited by PD7

As this is a homework assignment, I won't provide you with the the answer directly. What I will say is take a look at the methods in the Java.Math class. Some of them may be what you need.

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.