1th Week Programming Questions
View Help
return
Warm tips:
1. This assignment belongs to the online Judge topic and is enigmatic grading by the system immediately after submission.
2. Students can submit an unlimited number of answers before the deadline , and the system will take the highest score as the final result.
1th Week programming questions.
We give a part of the code in the topic description, you need to write the code according to the topic description, and then commit the two pieces of code together.
In accordance with the terms of academic integrity, I guarantee that this work is done independently.
1 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
time limit: 500ms memory limit: 32000kb
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 {public fraction (int a, int b) {//constructor while (b% A = = 0 && A! = 1) {//reduction fraction is 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_;} 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 numerator 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 gong Beishu = 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);} 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 the fraction to the simplest form b_/= a_;a_/= A_;} if (A_ = = 1 && b_ = = 1) {System.out.println (1);} else {System.out.println (A_ + "/" + B_);}} private int a_;private int b_;}
NetEase Cloud Classroom _java Language Programming _ 1th week class and object _1 Score