zoukankan      html  css  js  c++  java
  • 第四周课程总结&试验报告(二)

    计科一班 罗宇梁 20188381

    实验二 Java简单类与对象

    实验目的

    掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;
    理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;
    理解static修饰付对类、类成员变量及类方法的影响。
    

    实验内容

    1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色colour,width和height都是double型的,而color则是String类型的。要求该类具有:
      (1) 使用构造函数完成各属性的初始赋值
      (2) 使用get…()和set…()的形式完成属性的访问及修改
      (3) 提供计算面积的getArea()方法和计算周长的getLength()方法
      实验源码:
    package Rextangle;
    
    public class Rextangle {
    	
    	public static void main(String[] args) {
            Rectangle re = new Rectangle();
            re.get();
            double i =re.getArea();
            System.out.println(i);
            double k =re.getLength();
            System.out.println(k);
        }
    }
    
    class Rectangle {
        double width;
        double heigh;
        String color;
    
        Rectangle() {
            this.width=10;
            this.heigh=5;
            this.color="White";
        }
    
        public void get(){
            System.out.println("矩形的宽是"+this.width);
            System.out.println("矩形的高是"+this.heigh);
            System.out.println("矩形的颜色是"+this.color);
        }
    
        public void setwidth(double single) {
            this.width=single;
        }
    
        public void setheigh(double single) {
            this.heigh=single;
        }
    
        public void setcolor(String single) {
            this.color=single;
        }
    
        public double getArea() {
            return this.width*this.heigh;
        }
    
        public double getLength(){
            return this.width*2+this.heigh*2;
        }
    
    }
    

    实验结果:


    1. 银行的账户记录Account有账户的唯一性标识(11个长度的字符和数字的组合),用户的姓名,开户日期,账户密码(六位的数字,可以用0开头),当前的余 额。银行规定新开一个账户时,银行方面提供一个标识符、账户初始密码123456,客户提供姓名,开户时客户可以直接存入一笔初始账户金额,不提供时初始 余额为0。定义该类,并要求该类提供如下方法:存款、取款、变更密码、可以分别查询账户的标识、姓名、开户日期、当前余额等信息。
      实验过程:
      public class Main {
        public static void main(String[] args) {
            Account re = new Account("yanqing1234","YT",2019,9,17,"010614",999999.99);
            re.setAll();
            re.changebalance(0.01);
            re.setAll();
            re.changepsd("123456");
            re.setAll();
        }
    }
    
    class Account {
        String ID;            String psd;                int year;
        String name;          double balance;         int month;
                                                      int day;
        Account() {
            this.ID="yanqing1234";
            this.name="null";
            this.year=0;
            this.month=0;
            this.day=0;
            this.psd="null";
            this.balance=0;
        }
        Account(String ID, String name, int year, int month, int day, String psd, double balance) {
            this.ID       =   ID;
            this.name     =   name;
            this.year     =   year;
            this.month    =   month;
            this.day      =   day;
            this.psd      =   psd;
            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);
        }
    } 
    

    实验结果:


    第四周课程总结

    什么是集成开发环境(IDE)?

    1.讲究一站式开发,使用'这个环境即可,有提示纠错功能
    2.让软件开发更简单,更高效
    

    有IDE时候

    不需要安装jdk 
    不需要手动配置环境变量
    不需要用javac命令对java源文件编译
    使用IDE 有很多代码都不要写,自动生成
    java源程序语法错误马上有提示。
    

    描述一下软件开发的过程?

    1.程序员先观察现实世界,从现实世界当中寻找对象
    2.寻找到N个对象之后,发现所有对象都有共同特征
    3.程序员大脑形成一个模板{类}
    4.Java程序员可以通过代码表述一个类
    5.java程序中有了一个类的定义
    6.然后通过类就可以创建对象
    7.有了对象之后,就可以让对象直接协作起来形成一个系统
    

    String类两种实例方法

    1、直接赋值
    String str="hello"
    str是一个对象,此时hello 保存在堆内存当中。
    2、使用new关键字对象实例化
    String str =new String ("hello")
    String本身是一个类,类中都有构造方法

    StringBuffer类

    public StringBuffer()
    
    构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
    
    public StringBuffer(int capacity)
    
    指定容量的字符串缓冲区对象
    
    public StringBuffer(String str)
    
    指定字符串内容的字符串缓冲区对象
    

    StringBuffer的方法
    public int capacity()

    返回当前容量。	理论值
    
    public int length()
    
    返回长度(字符数)。 实际值
    

    字符串查找

    String提供了两种查找字符串的方法,即indexOf与lastIndexOf方法。
    1、indexOf(String s)
    该方法用于返回参数字符串s在指定字符串中首次出现的索引位置,当调用字符串的indexOf()方法时,会从当前字符串的开始位置搜索s的位置;如果没有检索到字符串s,该方法返回-1

    String str ="We are students";
    int size = str.indexOf("a"); // 变量size的值是3
    

    去除空格

    trim()方法返回字符串的副本,忽略前导空格和尾部空格。

    字符串替换

    replace()方法可实现将指定的字符或字符串替换成新的字符或字符串
    

    oldChar:要替换的字符或字符串

    newChar:用于替换原来字符串的内容

    如果要替换的字符oldChar在字符串中重复出现多次,replace()方法会将所有oldChar全部替换成newChar。需要注意的是,要替换的字符oldChar的大小写要与原字符串中字符的大小写保持一致。

    1 String str= "address";
    2 String newstr = str.replace("a", "A");// newstr的值为Address
    

    判断字符串是否相等

    1、equals(String otherstr)
    
    如果两个字符串具有相同的字符和长度,则使用equals()方法比较时,返回true。同时equals()方法比较时区分大小写。
    

    2、equalsIgnoreCase(String otherstr)

    equalsIgnoreCase()方法与equals()类型,不过在比较时忽略了大小写。
    

    字符串分割

    使用split()方法可以使字符串按指定的分隔字符或字符串对内容进行分割,并将分割后的结果存放在字符数组中。
    
    1 str.split(String sign);
    

    sign为分割字符串的分割符,也可以使用正则表达式。

    没有统一的对字符串进行分割的符号,如果想定义多个分割符,可使用符号“|”。例如,“,|=”表示分割符分别为“,”和“=”。

    1 str.split(String sign, in limit);
    

    该方法可根据给定的分割符对字符串进行拆分,并限定拆分的次数。

  • 相关阅读:
    iOS中3种正则表达式的使用
    iOS 正则表达式
    Autolayout-VFL语言添加约束-备
    PHP一个最简单的CMS内容管理系统
    国外主流PHP框架比较
    PHP中的ob_start() 的使用
    jpGraph的应用及基本安装配置 BY 命运
    未能加载文件或程序集“Common”或它的某一个依赖项。试图加载格式不正确的程序
    Android手机 Fildder真机抓包
    axWindowsMediaPlayer1获取音频长度
  • 原文地址:https://www.cnblogs.com/luoyuliang/p/11550332.html
Copyright © 2011-2022 走看看