Week 1th programming Questions-fraction__ programming for fractional classes

Source: Internet
Author: User
Tags static class
topic content

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

The constructors for this class are:


fraction (int a, int b)

Constructs a A/b score.


This class provides the following features:


Double ToDouble ();

Convert Fractions to Double

Fraction Plus (fraction r);

Add your own score and R's score to create a new fraction object. Notice how two scores are added to the grade four.

Fraction multiply (fraction r);

Multiplies your score and R's score to produce a new fraction object.

void print ();

Output to the standard output in the form of "numerator/denominator" 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 raise the integer part, i.e. 31/30 is the correct output.


Note that when you create and finish an operation, you should simplify the score to a minimalist form. If 2/4 should be reduced to 1/2.


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


Import Java.util.Scanner;


Publicclass Main {


Publicstaticvoid Main (String[]args) {

Scannerin = new Scanner (system.in);

Fractiona = new fraction (In.nextint (), In.nextint ());

FRACTIONB = 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 class classes.


input Format

The program runs with four digits, consisting 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 input and output.


Input Sample

2 4 1 3


Output Sample

1/2

1/3

5/6

1

1/2

1/3


Time limit: 500ms memory limit: 32000kb
The code is as follows

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 fenzi, FENMU;
		public fraction (int a, int b) {This.fenzi = A;
	This.fenmu = b;
	Public double ToDouble () {return fenzi * 1.0/FENMU;
		} public fraction plus (fraction r) {fraction m = new fraction (0, 1);
		M.FENMU = R.fenmu * FENMU;
		M.fenzi = Fenzi * R.FENMU + fenmu * R.FENZI;
	return m;
		Public fraction multiply (fraction r) {fraction m = new fraction (0, 1);
		M.FENMU = R.fenmu * FENMU;
		M.fenzi = Fenzi * R.fenzi;

	return m;
		} void print () {int r, x = fenmu, y = Fenzi;
			while (y!= 0) {r = x% y;
			x = y;
		y = r; } Fenzi/= X
		Fenmu/= x;
		/*if (fenzi% fenmu!= 0) System.out.println (fenzi + "/" + FENMU);
			else {int a = FENZI/FENMU;
		System.out.println (a);
		}*/if (FENZI==FENMU) System.out.println (FENZI/FENMU);
		else System.out.println (fenzi+ "/" +FENMU);
	Return
 }
}


Learning Experience
Previously written: public class main{public static void Main (string[] args) {}
Class fraction{}} will prompt for an error because the fraction class is an inner class defined in main. The fraction inner class is a dynamic inner class, while the main method is static.
Modify Method 1: Like the submitted version, write the class to the outside of Main.
Modify method 2:class before adding static
<pre name= "code" class= "Java" >package main;

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 ();

		public static class Fraction {private int fenzi, FENMU;
			public fraction (int a, int b) {This.fenzi = A;
		This.fenmu = b;
		Public double ToDouble () {return fenzi * 1.0/FENMU;
			} public fraction plus (fraction r) {fraction m = new fraction (0, 1);
			M.FENMU = R.fenmu * FENMU;
			M.fenzi = Fenzi * R.FENMU + fenmu * R.FENZI;
		return m;
			Public fraction multiply (fraction r) {fraction m = new fraction (0, 1);
			M.FENMU = R.fenmu * FENMU;
			M.fenzi = Fenzi * R.fenzi;

		return m; } void print () {int R, x = fenmu, y = Fenzi;
				while (y!= 0) {r = x% y;
				x = y;
			y = r;
			} fenzi/= x;
			Fenmu/= x;
			 * * IF (fenzi% fenmu!= 0) System.out.println (fenzi + "/" + FENMU); * else {int a = FENZI/FENMU; System.out.println (a);
			} */if (Fenzi = = Fenmu) System.out.println (FENZI/FENMU);
			else System.out.println (Fenzi + "/" + FENMU);
		Return
 }
	}

}


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.