Home > Back-end >  Java homework bug (4) for help
Java homework bug (4) for help

Time:12-11

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 the nextInt ());
Fraction b=new Fraction (in nextInt (), in the nextInt ());
a.print();
P. rint ();
Amy polumbo lus (b). Print ();
Arjun ultiply (b) plus (new Fraction (5, 6)). Print ();
a.print();
P. rint ();
in.close();
}
The class Fraction {
Fraction (int a, int b) {

}

Int a;
int b;

Double toDouble () {
Double the result;
Result=this. A/this. B;
Return (result);
}
Fraction plus Fraction (r) {
Aa, bb, int
Aa=of state Richard armitage + this. A;
Bb=r. b + this. B;
Fraction result=new Fraction (aa and bb);
return result;
}
Fraction multiply Fraction (r) {
Aa, bb, int
Aa=of state Richard armitage * this. A;
Bb=r. b * this. B;
Fraction result=new Fraction (aa and bb);
return result;
}
Void the print () {
Int n=this. GetGCD (enclosing a, this. B);
Int aa=this. A;
Aa=aa/n;
Int bb=this. B;
Bb=bb/n;
System. The out. Print (aa and bb);
}
Int getGCD (int a, int b) {
Int min=this. A & lt; This. B? This. A: this. B;
For (int I=min; I & gt;=1; I -) {
If (a % I==0 & amp; & B % I==0) {
return i;
}
The else {
return 1;
}
}
}
}
}

Thank you bosses

CodePudding user response:

Mend the topic request
Zhejiang university WengKai teacher longed for class assignments
Subject content:

Design a said scores of class Fraction, this class with two type int variables respectively and the denominator,

The constructor of this class is:



Fraction (int a, int b)

Constructs an a/b grade,



This class is to provide the following functions:



Double toDouble ();

Converts scores to double

Fraction plus Fraction (r);

Their grades and scores of r, create a new Fraction of object, pay attention to the fourth grade learned how two points together ha,

Fraction multiply Fraction (r);

Multiply their scores and r scores together, create a new Fraction of objects,

Void the print ();

Themselves in the form of "molecular/denominator" output to standard output, with a carriage return and line feed, if the score is 1/1, should the output 1, when molecules is greater than the denominator, don't need to put forward the integer part, namely 31/30 is a correct output,



Note that in creating and after operation should be reduced score is the simplest form, such as by 2/4 should be reduced to 1/2,



Class with the following code you wrote together, and please 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 the nextInt ());

Fraction b=new Fraction (in nextInt (), in the nextInt ());

a.print();

P. rint ();

Amy polumbo lus (b). Print ();

Arjun ultiply (b) plus (new Fraction (5, 6)). Print ();

a.print();

P. rint ();

in.close();

}



}



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



The class Fraction {



That is to say, in front of your class class don't have a public,



Input format:

Application runtime get four Numbers, constitute two points respectively, was followed by molecular and the denominator,



The output format:

Output some formula, the input and output are completed by the Main type of code, your code don't input and output,



Input the sample:

4 1 2 3



The output sample:

1/2

A third

5/6

1

1/2

A third

CodePudding user response:

Are you sure you're running the code posted above code is the same with you, I run the results did not appear in the static reference non-static member's error, as for your last error, System. Out. Print (aa and bb); Without this method, you should be to print out the result, rewritten into a string concatenation

CodePudding user response:

reference source on the second floor is a ghost reply:
are you sure you are running the code posted above code is the same with you, I run the results did not appear in the static reference non-static member's error, as for your last error, System. Out. Print (aa and bb); Without this method, you should be to print out the result, is rewritten into a string concatenation


1. I'm pretty sure this is the code
2. The bug I just solved it

CodePudding user response:

 
The class Fraction {
Private final int a;
Private final int b;

Fraction (int a, int b) {
Int maxDivisor=maxDivisor (a, b);
Enclosing a=a/maxDivisor;
This.=b/b maxDivisor;
}


Double toDouble () {
Return a * 1.0/b;
}

Fraction plus Fraction (r) {
Int a=enclosing a * r. b + this. B * state Richard armitage;
Int b=r. b * this. B;
Return new Fraction (a, b);
}

Fraction multiply Fraction (r) {
Int a=state Richard armitage * this. A;
Int b=r. b * this. B;
Return new Fraction (a, b);
}

Void the print () {
If (this. A==this. B) {
System. The out. Println (" 1 ");
return;
}

System. The out. Println (enclosing a + "/" + this. B);
}

Private int maxDivisor (int a, int b) {
Int Max=Math. Max (a, b);
Int min=Math. Min (a, b);

While (min!=0) {
Int temp %=Max min;
Max=min;
Min=temp;
}

The return of Max;
}
}

CodePudding user response:

reference 1/f, bitwise or bacteria response:
mend the topic request
Zhejiang university WengKai teacher longed for class assignments
Subject content:

Learn to stick program
 
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related