[Java first step, take a good unusual way] remember the first time eight children learn JAVA

This time last year, I also learn JAVA. A TV drama heirlooms formula textbook style and in addition surprisingly similar finishing Maogai outer notes, had nothing left.

Today, under the tutelage of MOOC Zhejiang Weng teacher. Not assiduously! Do not take notes! Look net pure class! Knock Code!

Joy! Comfortable! happy!

 

First Job title:

Title: Score

Title Contents:

Design a class that represents Fraction score. This class represents the two variables of type int numerator and denominator, respectively.

The constructor of this class are:

 

Fraction(int a, int b)

    A fraction of a configuration of a / b.

 

This class should provide the following features:

 

double toDouble();

    Convert scores into double

Fraction plus(Fraction r);

    The fraction of their scores and adding r to produce a new Fraction object. Fourth grade primary school learned how to pay attention to the sum of the two scores Kazakhstan.

Fraction multiply(Fraction r);

    The score your score and r is multiplied, to produce a new Fraction object.

void print();

    Their output in the form "numerator / denominator" to the standard output, and with a carriage return. If the score is 1/1, the output should be 1. When the molecule is greater than the denominator, we need to submit an integer part, i.e., 31/30 is a correct output.

 

Note that after the creation and operation should be done to simplify fraction to its simplest form. It should be simplified as 2/4 to 1/2.

 

And you write the following code to class together, and do not modify the 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 your class definition should start like this:

 

class Fraction {

 

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

 

Input formats:

Run time will be four numbers, each composed of two fractions, followed by the numerator and denominator.

 

Output formats:

Some output equation. These inputs and outputs are made Main class code completion, code that you do not do input and output.

 

Sample input:

2 4 1 3

 

Sample output:

1/2

1/3

5/6

1

1/2

1/3

 

Answers are as follows:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         Scanner in = new Scanner(System.in);    //用于控制端输入
 7         Fraction a = new Fraction(in.nextInt(), in.nextInt());
 8         Fraction b = new Fraction(in.nextInt(),in.nextInt());
 9         a.print();
10         b.print();
11         a.plus(b).print();
12         a.multiply(b).plus(new Fraction(5,6)).print();
13         a.print();
14         b.print();
15         in.close();
16     }
17 
18 }
19 
20 class Fraction{
21     int a;
22     int b;
23 
24     Fraction(int a,int b)   //构造函数
25     {
26         this.a = a;
27         this.b = b;
28         int m = gcd(a,b);   //Find the greatest common divisor of a and b 
29          the this II.A / = m;
 30          the this .B / = m;
 31 is  
32      }
 33 is  
34 is      int GCD ( int a, int b)
 35      {
 36          IF (b == 0 )
 37 [              return A;
 38 is          the else 
39          {
 40              return GCD (B, A% B);
 41 is          }
 42 is      }
 43 is  
44 is      Fraction PLUS (R & lt Fraction)
 45      {
 46 is          Fraction = mnew Fraction(0,1);
47         m.b = this.b * r.b;
48         m.a = this.a * r.b + r.a * this.b;
49         int n = gcd(m.a,m.b);
50         m.a /= n;
51         m.b /= n;
52         return m;
53     }
54 
55    Fraction multiply(Fraction r)
56    {
57        Fraction m = new Fraction(0,1);
58        m.a = this.a * r.a;
59        m.b = this.b * r.b;
60        int n = gcd(m.a,m.b);
61        m.a /= n;
62        m.b /= n;
63        return m;
64    }
65 
66    void print()
67    {
68        if(this.b == 1)
69            System.out.println(this.a);
70        else
71            System.out.println(this.a + "/" + this.b);
72    }
73 }

 

Guess you like

Origin www.cnblogs.com/Evanscabin/p/11250049.html