1: Method Characteristics of Classes
Methods in this class can be called directly by other methods
2: Membership variables and local variables
Membership variables: defined in the class, outside the method, the scope of action throughout the class, with initial values
Local variables: defined in the method, the scope of action in its own method, other methods in this class can not identify, no initial value,
You need to set it manually. Different methods can define local variables with the same name.
Note: Local variables and member variables have the same name
Local variables defined in the method have the highest priority
If you need to call a member variable of this class, you can use the keyword this
1. Object-oriented:
package day5;
/*
*/* Object-oriented
*
* Class class name
* Define the attribute part
* Type attribute 1 of attribute 1
* Type attribute 2 of attribute 2
*
* Defining Method Section
* method 1
* method 2
*
* Create Objects
* Class name object name = new class name
*scanner input = new scanner(system.in);
* Reference Object Members: Use ".".
* Object names. properties
* Object name. Method ()
*
* Static method:
*
* Class name. Method name ();
*
*
* Non-static methods:
*
* Create Objects
* Object name. Method name ();
*
*
*
*
*
*
*
*/
public class School { //Attribute member variable String name;//Name of center int classNum;//Number of classrooms int labNum;//Number of computer rooms //Method Membership Method public void show() { //code block System.out.println("The name of the center is:"+name+",Number of classrooms:"+classNum+",Number of computer rooms:"+labNum); } } package day5; //test public class Schooltest { public static void main(String[] args) { // create object School center = new School(); //Output before assignment System.out.println("Output before assignment"); center.show(); //Assignment object attributes center.name="Kazimen Campus"; center.classNum=10; center.labNum=8; System.out.println("Output after assignment"); center.show(); } }
2. Ticket cases
package day5;
import java.util.Scanner; public class Person { //attribute String name; int age ; //Method public void show() { Scanner input =new Scanner(System.in); while (!"n".equals(name)) { if (age>=18&&age<=60) { System.out.println("Ticket price 20"); } else { System.out.println("Admission free"); } //Cyclic input System.out.println("Please enter your name."); name=input.next(); if (!"n".equals(name)) { System.out.println("Please enter age"); age=input.nextInt(); } } System.out.println("Exit procedure"); } }
test
package day5; import java.util.Scanner; public class Person1 { public static void main(String[] args) { //Creating human objects Scanner input= new Scanner(System.in); Person per =new Person(); System.out.println("Please enter your name:"); per.name=input.next(); System.out.println("Please enter age:"); per.age=input.nextInt(); per.show(); } }
3. Toy cases:
package day5; /* * Lions * * * */ public class Lion { //attribute String color="yellow"; int a =10; //Method run public void run() { int a=20; System.out.println("With 0.1/The speed of seconds gallops forward"); } //Method Return Ball public String[] ball() { String [] b= {"Table Tennis","Tennis","Badminton"}; return b; } //Return color public String getcolor() { return color; } //Method output information returns a string public String showinfo() { return "One"+getcolor()+"Lion playing"+ball(); } }
Test:
package day5; public class Liontest { public static void main(String[] args) { // create object Lion li=new Lion(); System.out.println(li.showinfo()); } }
Homework:
1. Average score problem:
package day5homework; import java.util.Scanner; //Calculate average score and total score public class Avg { int javascores; int Cscores; int DBscores; int sum=0; double a; public void show() { Scanner input =new Scanner(System.in); sum=javascores+Cscores+DBscores; System.out.println("The total score is: "+sum); a=(javascores+Cscores+DBscores)/3; System.out.println("The average score is:"+a); } }
Test:
package day5homework; import java.util.Scanner; public class Avgtest { public static void main(String[] args) { Scanner input =new Scanner(System.in); Avg ag= new Avg(); System.out.println("Please input java Results:"); ag.javascores=input.nextInt(); System.out.println("Please input C#Results: ""; ag.Cscores=input.nextInt(); System.out.println("Please input DB achievement:"); ag.DBscores=input.nextInt(); ag.show(); } }
2. User login:
package day5homework; /* * Change Administrator Password * * * */ import java.util.Scanner; public class passwd { String name="admin1"; int passwd=111111; int newpasswd; //Method public void show() { Scanner input =new Scanner(System.in); if (name.equals("admin1")&&passwd==111111) { System.out.println("Please enter a new password:"); newpasswd=input.nextInt(); System.out.println("Successful password modification, your new password is"+newpasswd); } else { System.out.println("User name and password do not match! You do not have permission to update administrator information"); } } } //Test: package day5homework; import java.util.Scanner; public class Passwdtest { public static void main(String[] args) { Scanner input= new Scanner(System.in); passwd ps =new passwd(); System.out.println("Please enter a user name:"); ps.name=input.next(); System.out.println("Please input a password:"); ps.passwd=input.nextInt(); ps.show(); } }
3. Shopping problems:
package day5homework; import java.util.Scanner; public class Shopping { int num=1; String name="JadeBird"; int passwd =0000; //Method public void show() { Scanner input =new Scanner(System.in); if(num!=1){ System.out.println("Sign out"); } else { // System.out.println("Please enter user name:"); // System.out.println("Please enter your password:"); passwd=input.nextInt(); if (name.equals("jadeBird")&&passwd==0000) { System.out.println("@@Landing success: JadeBird@@"); } else { System.out.println("You do not have the right to log on to the system. Please log on again. @@"); } } } }
Test:
package day5homework; import java.util.Scanner; public class Shoppingtest { public static void main(String[] args) { Scanner input =new Scanner(System.in); Shopping sp=new Shopping(); System.out.println("Welcome to use our own shopping management system"); System.out.println("1.Login system"); System.out.println("2.Sign out"); System.out.println("**********************"); System.out.println("Please select and enter the number:"); sp.num=input.nextInt(); System.out.println("Please enter a user name:"); sp.name=input.next(); System.out.println("Please input a password:"); sp.passwd=input.nextInt(); sp.show(); }