第四周课程总结

  1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

(2) 使用get…()和set…()的形式完成属性的访问及修改

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

实验代码:

 

package test;

public class Rectangle {
private double width,height;
    
private String color;
    
public Rectangle(double width,double height,String color){
        
this.setWidth(width);
        
this.setHeight(height);
        
this.setColor(color);
    
}
    
public double getWidth(){
        
return width;
    
}
    
public void setWidth(double width){
        
this.width = width;
    
}
    
public double getHeight(){
        
return height;
   
}
    
public void setHeight(double height){
        
this.height = height;
    
}
    
public String getColor(){
        
return color;
    
}
    
public void setColor(String color){
        
this.color = color;
    
}       
    
public void getArea(){
        
double area=0;
        
area=this.height*this.width;
        
System.out.println(area);
        
}
    
public void getLength(){
        
double length;
        
length=(width+height)*2;
         
System.out.println(length);
        
 }
    
public String toString(){
     
String rec="高:"+this.getHeight()+"宽:"+this.getWidth()
     +"颜色:"+this.getColor();
     return rec;
     
}
    
public static void main(String[] args) {
     
Rectangle rec=new Rectangle(20, 10, "蓝色");
     
rec.getArea();
     
rec.getLength();
     
System.out.println(rec.toString());
     
}
     
}

错误截屏

正确截屏

2、银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。

实验代码:

package text;
import java.util.Scanner;
import java.util.Date;
public class Account {
 
 private String id;
 private String name;
 private Date date;
 private int password;
 private int money;
 
 public Account(String id, String name, int money) {
  super();
  this.id = id;
  this.name = name;
  this.money = money;
  this.date = new Date();
  this.password = 123456;
 }
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public void into(int num) {
  this.money = this.money+num;
 }
 public void out(int num) {
  this.money = this.money-num;
 }
 
 public Date getDate() {
  return date;
 }
 public void setDate(Date date) {
  this.date = date;
 }
 public int getPassword() {
   return password;
  }
 public void setPassword(int password) {
   this.password = password;
  }
  
 public int getMoney() {
  return money;
 }
 public void setMoney(int money) {
  this.money = money;
 }
 
 public void newpassword() {
  Scanner sc=new Scanner(System.in);
     int password=sc.nextInt();
     this.password = password;
     sc.close();
    
 }
 public String toString(){
 String str="账户名:"+this.getId()+" 姓名:"+this.getName()+"开户日期:"+this.getDate()+" 当前余额:"+this.getMoney();
 return str;
 }
}
 
 

这题目真的写不出来,我太难了,题目太难了,只能把我能写的写一些了。

总结:这周学了import语句跟包概念,不是很能理解,但是可以照着书可以写出来,可以运用。Java好难,希望能够早点能灵活运用,当一名合格的程序员,哈哈哈哈哈。

     

原文地址:https://www.cnblogs.com/Bowen----/p/11559956.html