第四周作业及总结

实验二 Java简单类与对象

实验目的
掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
理解static修饰付对类、类成员变量及类方法的影响。
实验内容
写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
(1) 使用构造函数完成各属性的初始赋值

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

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

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

(1)

public class Rectangle {
    private double width;    
    private double height;
    private String color;
    
    public Rectangle(double width, double height, String color) {
        this.setWidth(width);
        this.setHeight(height);   
        this.setColor(color);
    }
    public Rectangle() {
        // TODO Auto-generated constructor stub  
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public void setColor(String color) {  
        this.color = color;
    }
    public double getWidth() {
        return width;
    }
    public double getHeight() {
        return height;
    }
    public String getColor() {       
        return color;
    }
    public double getArea() {
        return this.width*this.height; 
    }
    public double getGetlength() {
        return (this.height+this.width)*2;
    }
    public static void main(String[] args) {
        Rectangle a=null;
        a = new Rectangle(4,5,"red");  

        System.out.println(a.getArea());  
        //System.out.println(+a.getGetlength());
    }
}

作业报错,包括eclipse配置突然出现问题所以还是用的jdk,但是总是有个问题没搞懂

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

public class Bank {
	public static void main(String[] args) {
        Account a = new Account();
        a.setAll();
        a.changebalance(1000.0);
        a.setAll();
        a.changepsd("123456");
        a.setAll();
    }
}
class Account {
    String ID;
    String psd;
    String name; 
    int year;
    int month;
    int day;
    double balance;         
    Account() {
        this.ID="1sunzeliang";
        this.name="szl";
        this.psd="195315";
        this.year=2008;
        this.month=8;
        this.day=8;
        this.balance=10000.0;
    }
    Account(String ID, String name, String psd, int year, int month, int day, double balance) {
        this.ID = ID;
        this.name = name;
        this.psd = psd;
        this.year = year;
        this.month = month;
        this.day = day;
        this.balance = balance;
    }
    public void changebalance(double single) {
        this.balance += single;
    }
    public void changepsd(String single) {
        this.psd = single;
    }
    public void setAll() {
        System.out.println(this.ID);
        System.out.println(this.name);
        System.out.println(this.year+"."+this.month+"."+this.day);
        System.out.println(+this.balance);
    }
}

总结
本周学习了string类的常用操作方法
字符串中包含的字符数,也就是字符串的长度。

int length():获取长度。

根据指定位置获取位置上的某个字符

char charAt(int index)

根据字符获取该字符在字符串中的位置

Int indexOf(int ch):返回的是ch在字符串中第一次出现的位置
int indexOf(int ch,int fromIndex):从fromIndex指定位置开始,获取ch在字符串中出现的位置
int indexOf(String str,int fromIndex): 从fromIndex指定位置开始,获取ch在字符串中出现的位置,如果没有找到返回-1
int lastIndexOf(String str):反向索引一个字符出现的位置

等方法和Java的一些常用包
包名

内容概述

Java.applet

提供创建applet小程序所需要的类

Java.awt

包含用于创建用户界面和绘制图形图像的所有类

Java.io

提供与输入输出相关的类

Java.beans

包含与开发javaBeans相关的类

Java.lang

提供java语言程序设计的基础类

Java.net

提供实现网络操作相关的类

Java.nio

为输入输出提供缓冲区的类

Java.text

提供处理文本、日期、数字和消息的类和接口

Java.util

提供处理日期、时间、随机数生成等各种使用工具的类

Javax.net

提供用于网络应用程序的类、网络应用扩展类

Java.swing

提供一组与AWT功能相同的纯java的组件类
原文地址:https://www.cnblogs.com/zou1223/p/11556934.html