China MOOC score--java

Source: Internet
Author: User

Score (10 points)

Topic content:

Design a class fraction that represents fractions. This class uses two variables of type int to represent the numerator and denominator, respectively.

The constructors for this class are:

fraction (int a, int b)

Constructs a A/b score.

This class provides the following functionality:

Double ToDouble ();

Convert a fraction to double

Fraction Plus (fraction r);

Add your score and R's score to create a new fraction object. Notice how the two grades in grade four have been added.

Fraction multiply (fraction r);

Multiply your score by the score of R and generate a new fraction object.

void print ();

Export yourself as a "numerator/denominator" to the standard output with a carriage return line. If the score is 1/1, you should output 1. When the numerator is greater than the denominator, there is no need to propose an integer part, i.e. 31/30 is the correct output.

Note that after the creation and completion of the operation, the reduction score should be reduced to the simplest form. If 2/4 should be reduced to 1/2.

The classes you write should be put together with the following code, and do not modify this code:

Import Java.util.Scanner;

public class Main {

public static void Main (string[] args) {

Scanner in = new Scanner (system.in);

Fraction a = new fraction (In.nextint (), In.nextint ());

Fraction b = new fraction (In.nextint (), In.nextint ());

A.print ();

B.print ();

A.plus (b). print ();

A.multiply (b). Plus (new fraction (5,6)). Print ();

A.print ();

B.print ();

In.close ();

}

}

Note that the definition of your class should start like this:

Class Fraction {

In other words, do not have public in front of your class classes.

Input format:

When the program runs, it gets four numbers, each of which consists of two fractions, followed by numerator and denominator.

Output format:

Output some formulas. These inputs and outputs are done by the code of the main class, and your code does not do input and output.

Input Sample:

2 4 1 3

Sample output:

1/2

1/3

5/6

1

1/2

1/3

SOURCE Display:

Import Java.util.Scanner;
public class Main {
public static void Main (string[] args) {
Scanner in = new Scanner (system.in);
Fraction a = new fraction (In.nextint (), In.nextint ());
Fraction b = new fraction (In.nextint (), In.nextint ());
A.print ();
B.print ();
A.plus (b). print ();
A.multiply (b). Plus (new fraction (5,6)). Print ();
A.print ();
B.print ();
In.close ();
}
}
Class Fraction {
private int a_;//molecule
private int b_;//Denominator

public fraction (int a, int b) {//constructor
while (b% A = = 0 && A! = 1) {//simplify fractions to the simplest form
b/= A;
A/= A;
}

A_ = A;
B_ = b;
}

public int Geta () {//Get molecule
return a_;
}

public int Getb () {//Get denominator
return b_;
}

Public fraction plus (fraction r) {//adds its own score and R's score to produce a new fraction object. Notice how the two grades in grade four have been added.
int a_1 = a_;//First molecule
int b_1 = b_;//First Denominator
int a_2 = R.geta ();//second molecule
int b_2 = R.GETB ();//second denominator
int c = b_1 * b_2;
int Gongbeishu = 0;

if (B_1 < b_2) {
int temp = b_1;
b_1 = b_2;
b_2 = temp;
}

while (true) {
int temp = b_1% b_2;

if (temp = = 0) {
Gongbeishu = c/b_2;
Break
} else {
b_1 = b_2;
b_2 = temp;
}
}

if (b_1! = Gongbeishu) {
int Beishu = gongbeishu/b_;
a_1 = Beishu * A_;
b_1 = Beishu * B_;
}

if (b_2! = Gongbeishu) {
int Beishu = Gongbeishu/(R.GETB ());
a_2 = Beishu * R.geta ();
b_2 = Beishu * R.GETB ();
}

return new fraction (a_1 + a_2, b_1);
}

Public fraction multiply (fraction r) {//multiplies its score and R's fraction to produce a new fraction object.
return new Fraction (A_ * (R.geta ()), B_ * (R.GETB ()));
}

public void print () {//outputs itself as a "numerator/denominator" to the standard output with a carriage return with a newline. If the score is 1/1, you should output 1. When the numerator is greater than the denominator, there is no need to propose an integer part, i.e. 31/30 is the correct output.
while (b_% A_ = = 0 && A_! = 1) {//simplify fractions to the simplest form
B_/= A_;
A_/= A_;
}

if (A_ = = 1 && b_ = = 1) {
SYSTEM.OUT.PRINTLN (1);
} else {
System.out.println (A_ + "/" + b_);
}
}
}

China MOOC score--java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.