The fourth week Summary & test report fourth week course (two)

Summary & test report fourth week course (two)

 

A. Write a rectangle representing a class named Rectangle. Attributes which includes a wide width, height, and color of high color, width and height are double-type, and the color is of type String.

Requirements class has:

(1) using the constructor completes the initial assignment of each attribute

(2) using the get ... () and set ... () in the form of complete access and modify the properties of

(3) calculate the area of ​​providing getArea () method and the calculated circumference getLength () method

 

Experiment Code

Copy the code
class Rectangle // class name defined rectangular 
{ 
    Private Double width; // rectangle width 
    private double height; // height of the rectangle 
    private String color; // color rectangle 
    
class Rectangle // class name defined rectangular 
{ 
    Private Double width; // rectangle width 
    private double height; // height of the rectangle 
    private String color; // color rectangle 
    
    public rectangle () // defines constructor with no arguments 
    {      
    } 
// constructor is defined with three parameters, attributes for the class initialization 
public rectangle (double width, height Double, String color) 
{ 
    this.setWidth (width); // set the width 
    this.setHeight (height); // set the height 
    this.setColor (color); // set the color 
}
   public void setWidth (double s) // set the width 
   { 
       width = S;        
   } 
   public void setHeight (n-Double) // sets the length 
   { 
       height = n-; 
   } 
   public void setColor (String Y) // set the color 
   { 
       Color = Y; 
   } 
   public Double getWidth () // number obtain 
   { 
       return width; 
   } 
   public Double getHeight () // obtain highly 
   { 
       return height; 
   } 
   public String the getColor () // color obtain 
   { 
       return color; 
   } 
   public Double the getArea () // calculating the area 
   { 
       return height * width; 
   } 
   public Double getLength () // calculate the perimeter 
   {
       return (height+width)*2;
   }
}
Copy the code

 

 

 

II. Bank account records the unique identification of Account have an account (a combination of characters and numbers 11 lengths), user name, date of opening the account password (six-digit number that can start with 0), the current balance. Bank of when to open a new account, the bank provides an identifier, the initial account password 123456, customers provide your name, customers can open an account when a deposit for an initial account amount, without providing an initial balance of zero. Definition of the class and ask the class provides the following methods: deposits, withdrawals, change your password, you can query separately identifies an account, name, date of opening, the current balance and other information.

 

Experiment Code

Copy the code
import java.util.Date;
import java.util Scanner;
public class bank
{
    private String id;
    private String name;
    private double createtime;
    private String password;
    private int money;
    
    public amount(String id,String name,int money)
    {
        this.setID(id);
        this.setName(name);
        this.setMoney(money);
        this.setPassword="123456";
        this.setCreatetime = new Date();
    }
    public int deposit()
    {
        return money+amount;
    }
    public int withdraw()
    {
        return amount-money;
    }
    public void setCreatetime(int s)
    {
        createtime = s;
    }
    public int getcreatetime()
    {
        return createtime;
    }
    public void changepassword()
    {
      Scanner x = new Scanner(System.in);
      System.out.println("输入新的密码:");
      String password = x.nextString();
      this.password = new password;
    }
    public static void main(String [] args)
    {
        Bank n = new bank("id","name","createtime");
        System.out.println("账号:"+n.getID());
        System.out.println("姓名:"+n.getName());
        System.out.println ( "opening date:" + n.getCreatetime ()); 
        System.out.println("开户日期:"+n.getCreatetime());
        System.out.println ( "Balance:" + n.getAmont ()); 
    } 
}
Copy the code

 

Note: The above code represent the personal ideas.

                     

Experimental Summary:

A. When we create a class, the class will have more than one object, the object must be correctly instantiated before it can be used.

Examples of the effects:

1. Check whether the class loader

2. Allocate space parent type, in accordance with the spatial distribution properties, property is automatically initialized to "zero"

These two roles that I know, of course, I say may be incomplete, hope to be able to add the complete

 

II. Why is private property to be used with the set and get methods to set access?

Separating data and behavior, the object reflects the encapsulation, encapsulation property privatization is to provide a method of public access to private property. I understand that everyone has a specific name with a particular character, can not be freely modified, can only be modified by a specific method, we will declare member variables private, and then to access this variable by means of public

 

three.

I let a mistake is often the type of property previously defined class time after the call to make sure the properties of this class, we must ensure that the type of guarantee consistent with the previous

 

A. Write a rectangle representing a class named Rectangle. Attributes which includes a wide width, height, and color of high color, width and height are double-type, and the color is of type String.

Requirements class has:

(1) using the constructor completes the initial assignment of each attribute

(2) using the get ... () and set ... () in the form of complete access and modify the properties of

(3) calculate the area of ​​providing getArea () method and the calculated circumference getLength () method

 

Experiment Code

Copy the code
class Rectangle // class name defined rectangular 
{ 
    Private Double width; // rectangle width 
    private double height; // height of the rectangle 
    private String color; // color rectangle 
    
class Rectangle // class name defined rectangular 
{ 
    Private Double width; // rectangle width 
    private double height; // height of the rectangle 
    private String color; // color rectangle 
    
    public rectangle () // defines constructor with no arguments 
    {      
    } 
// constructor is defined with three parameters, attributes for the class initialization 
public rectangle (double width, height Double, String color) 
{ 
    this.setWidth (width); // set the width 
    this.setHeight (height); // set the height 
    this.setColor (color); // set the color 
}
   public void setWidth (double s) // set the width 
   { 
       width = S;        
   } 
   public void setHeight (n-Double) // sets the length 
   { 
       height = n-; 
   } 
   public void setColor (String Y) // set the color 
   { 
       Color = Y; 
   } 
   public Double getWidth () // number obtain 
   { 
       return width; 
   } 
   public Double getHeight () // obtain highly 
   { 
       return height; 
   } 
   public String the getColor () // color obtain 
   { 
       return color; 
   } 
   public Double the getArea () // calculating the area 
   { 
       return height * width; 
   } 
   public Double getLength () // calculate the perimeter 
   {
       return (height+width)*2;
   }
}
Copy the code

 

 

 

II. Bank account records the unique identification of Account have an account (a combination of characters and numbers 11 lengths), user name, date of opening the account password (six-digit number that can start with 0), the current balance. Bank of when to open a new account, the bank provides an identifier, the initial account password 123456, customers provide your name, customers can open an account when a deposit for an initial account amount, without providing an initial balance of zero. Definition of the class and ask the class provides the following methods: deposits, withdrawals, change your password, you can query separately identifies an account, name, date of opening, the current balance and other information.

 

Experiment Code

Copy the code
import java.util.Date;
import java.util Scanner;
public class bank
{
    private String id;
    private String name;
    private double createtime;
    private String password;
    private int money;
    
    public amount(String id,String name,int money)
    {
        this.setID(id);
        this.setName(name);
        this.setMoney(money);
        this.setPassword="123456";
        this.setCreatetime = new Date();
    }
    public int deposit()
    {
        return money+amount;
    }
    public int withdraw()
    {
        return amount-money;
    }
    public void setCreatetime(int s)
    {
        createtime = s;
    }
    public int getcreatetime()
    {
        return createtime;
    }
    public void changepassword()
    {
      Scanner x = new Scanner(System.in);
      System.out.println("输入新的密码:");
      String password = x.nextString();
      this.password = new password;
    }
    public static void main(String [] args)
    {
        Bank n = new bank("id","name","createtime");
        System.out.println("账号:"+n.getID());
        System.out.println("姓名:"+n.getName());
        System.out.println ( "opening date:" + n.getCreatetime ()); 
        System.out.println ( "Balance:" + n.getAmont ()); 
    } 
}
Copy the code

 

Note: The above code represent the personal ideas.

                     

Experimental Summary:

A. When we create a class, the class will have more than one object, the object must be correctly instantiated before it can be used.

Examples of the effects:

1. Check whether the class loader

2. Allocate space parent type, in accordance with the spatial distribution properties, property is automatically initialized to "zero"

These two roles that I know, of course, I say may be incomplete, hope to be able to add the complete

 

II. Why is private property to be used with the set and get methods to set access?

Separating data and behavior, the object reflects the encapsulation, encapsulation property privatization is to provide a method of public access to private property. I understand that everyone has a specific name with a particular character, can not be freely modified, can only be modified by a specific method, we will declare member variables private, and then to access this variable by means of public

 

three.

I let a mistake is often the type of property previously defined class time after the call to make sure the properties of this class, we must ensure that the type of guarantee consistent with the previous

 

Guess you like

Origin www.cnblogs.com/0605syh/p/11546077.html