1, Making objects with classes
Objects are entities that need to be created to do things for us
A class is a specification that creates objects according to the definition of a class
Object = attribute + Service
Encapsulation: putting data and operations on data together
2, Define class
Create object: new VendingMachine; Or VendingMachine vm = new VendingMachine;
The object variable is the manager of the object
Let the object do things: "." operator
3, Member variables and member functions
Class defines all variables in the object and becomes member variables
Each object has its own variables, which are separate from other objects of the same class
Functions are called through objects
This is a special intrinsic local variable of the member function, which represents the object that calls the function this time
Call function: call the function of an object through the "." operator, or directly call other functions of this within the member function
The lifetime of a member variable is the lifetime of an object, and the scope is a member function within a class
4, Object initialization
Member variables are automatically initialized or assigned
Constructor: if the name of a member variable is exactly the same as that of a class, this function will be called automatically when each object of this class is created. This function cannot have a return type
Function overloading: a class can have multiple constructors as long as their parameter tables are different. When you give different parameter values when creating an object, you will automatically call different constructors. Other constructors can also be called through this(). Functions with the same name but different parameter tables in a class form an overload relationship
5, Score example
Title Content:
Design a class Fraction that represents scores. This class uses two variables of type int to represent numerator and denominator respectively.
The constructor of this class is:
Fraction(int a, int b)
Construct a fraction of a/b.
This class should provide the following functions:
double toDouble();
Convert score to double
Fraction plus(Fraction r);
Add your own score and r's score to produce a new Fraction object. Pay attention to how the two scores are added in the fourth grade of primary school.
Fraction multiply(Fraction r);
Multiply your score by the score of r to produce a new Fraction object.
void print();
Output yourself to standard output in the form of "numerator / denominator", with carriage return and line feed. If the score is 1 / 1, 1 should be output. When the numerator is greater than the denominator, there is no need to put forward the integer part, that is, 31 / 30 is a correct output.
Note that after creating and completing the operation, the score should be reduced to the simplest form. For example, 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;
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, don't have public in front of your class.
Input format:
When the program runs, four numbers will be obtained to form two fractions, which are numerator and denominator in turn.
Output format:
Output some formulas. These inputs and outputs are completed by the code of the Main class. Do not input and output your code.
Input example:
2 4 1 3
Output example:
1/2
1/3
5/6
1
1/2
1/3
package lianxi; import java.util.Scanner; public class fenshu { 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{ int fenzi; int fenmu; Fraction(int a,int b){ this.fenzi = a; this.fenmu = b; } double toDouble() { return (double)fenzi/fenmu; } Fraction plus(Fraction r) { Fraction temp = new Fraction(0,0); temp.fenzi = fenzi*r.fenmu+fenmu*r.fenzi; temp.fenmu = fenmu*r.fenmu; return temp; } Fraction multiply(Fraction r) { Fraction temp = new Fraction(0,0); temp.fenzi = fenzi*r.fenzi; temp.fenmu = fenmu*r.fenmu; return temp; } void print() { int n; int m=0; if(fenzi==fenmu) { System.out.println(1); } else { if(fenzi>fenmu) { n=fenmu; } else { n=fenzi; } for(int i=1;i<=n;i++) { if(fenzi%i==0&&fenmu%i==0) { m=i; } } fenzi=fenzi/m; fenmu=fenmu/m; System.out.println(fenzi+"/"+fenmu); } } }