Java Experiment 5: Classes and Objects

  • Experiment name

classes and objects

  • Experiment purpose and requirements (knowledge points involved and required to be mastered in this computer experiment)
  1. Proficient in the definition of classes;
  2. Proficient in the creation and use of objects;
  3. Definition and access of class variables and static methods.
  • Experimental environment (the platform and related software used in this computer practice)

Multimedia microcomputer; Windows, jdk and Eclipse.

  • experimental design
  1. Experiment content

(1) Create a class named Rectangle to represent a rectangle. This class contains two attributes: length and width, including: completing the definition of the constructor methods of the Rectangle class, and using these constructor methods to create objects respectively;

One finds the area (getArea());

  A method to find the perimeter (getPerimeter()).

Create the object and test the two methods getArea() and getPerimeter().

(2) Define a class to implement a bank account (BankAccount class), including attributes such as account number and deposit balance money, and methods including: construction, deposit, withdrawal, balance query and account number display. Create several account class objects and complete the test of four methods including deposits.

  1. Experimental steps
  1. Program 1
 package shiyan5;

import java.util.Scanner;

public class shiyan5_1 {

public static void main(String[] args) {

Rectangle rect = new Rectangle();

Scanner in=new Scanner(System.in);

System.out.println("请输入长和宽:");

rect.width = in.nextInt();

rect.height = in.nextInt();

System.out.println(rect.getArea()+" "+rect.getPerimeter());

in.close();

}

}

class Rectangle{



int height;

int width;

public int getArea()

{



return this.width*this.height;



}

public int getPerimeter()

{

System.out.println("面积和周长为:");

return (this.width+this.height)*2;



}

}

  1. Program 2


 package shiyan5;

import java.util.Scanner;

public class BankAccount {

String name;

int ID;

int password;

int money;

public BankAccount(String name,int ID,int password,int money){

this.name=name;

this.ID=ID;

this.password=password;

this.money=money;

}

public void Display(){

System.out.println("账户:"+ID);

System.out.println("姓名:"+name);

System.out.println("余额:"+money);

}

public void takeMoney(){

while(true){

Scanner sc=new Scanner(System.in);

System.out.println("请输入密码进行验证!");

int pass=sc.nextInt();

if(pass==password){

System.out.println("请输入需要取款的金额!");

int withdrawals=sc.nextInt();

if(withdrawals<=money){

money=money-withdrawals;

System.out.println("账户余额"+money);

}

else{

System.out.println("当前余额不足!");

}

break;

}

else{

System.out.println("你输入的密码有误,请重新输入!");

}

sc.close();

}

}

public void saveMoney(int inmoney){

money=money+inmoney;

System.out.println("此次存款为:"+inmoney);

System.out.println("账户余额:"+money);

}

public void display(){

System.out.println("账号显示"+ID);

}

public static void main(String[] args) {

BankAccount people=new BankAccount("dsd",12345,12345,2000);

people.Display();

people.saveMoney(2000);

people.takeMoney();

people.display();



}

Guess you like

Origin blog.csdn.net/MYSELFWJC/article/details/131803141