• 第四周总结 & 实验报告(二)


    第四周课程总结

    一、String类

     

     

    1.实例化

    (1)直接赋值

    public class Xxxx{
              public static void main(String args[]){
                         String a = "abcd";              //实例化String对象
    
                         System.out.println(a);          //输出
               }
          
    }

    (2)使用new关键词

    public class Xxxx{
              public static void main(String args[]){
                         String a = new String("abcd");              //实例化String对象
                         System.out.println(a);                         //输出
               }
    }

    2.String 对象的内容比较

    1)使用“==”比较

                    进行数值的比较,即是用来进行地址值的比较

    (2)使用equals()方法比较

                 进行内容的比较

                 使用方法:

                  例如:str1.equals(str2); 

    3.两种实例化方式的区别

    (1)直接赋值

              String xxx = "字符串";

              只会开辟一块堆内存空间,并且会自动保存在对象池之中一共下次使用;

    (2)构造方法

              String xxx = new String ("字符串");

               会开辟两块堆内存空间,其中有一块空间将成为垃圾,并且不会自动入池,但是用户可以使用intern()方法手工入池;

    4.字符串内容不可改变

         

       可以使用StringBuffer类完成(第十三章)

    5.常用方法

    (1)字符串与字符数组的转换

    String str1 = "hello";
    char c[] = str1.toCharArray();

    (2)取出指定位置字符

    String str1 = "hello";
    System.out.println("str1.charAt(下标)");        //取出第 下标+1 个字符

    (3)字符串与byte数组的转换

    String str1 = “hello”;
    byte b[] = str1.getBytes();                             //将字符串变为byte数组
    System.out.println(new String(b));                 //将全部byte数组变为字符串
    System.out.println(new String(b,1,3));          //将部分byte数组变为字符串
    
    
    
    
    执行结果:hello
            ell

     (4)取得一个字符串的长度

    String str1 = "hello";
    System.out.println(str1.length());

    (5)查找一个指定的字符串是否存在

    String str1 = "abcdefgh";
    System.out.println(str1.indexOf("c"));             //查找到返回位置
    System.out.println(str1.indexOf("c",3));          //查找到返回位置,从第四个开始查找
    System.out.println(str1.indexOf("x"));            //没有找到返回-1

    (6)去掉左右空格

    String str1 = "         hello          ";
    System.out.println(str1.trim());

    (7)字符串截取

    String str1 = "hello world";
    System.out.println(str1.substring(6));
    System.out.println(str1.substring(0,5));

    (8)按照指定的字符串拆分字符串

    String str1 = “hello world”;
    String s[] = str1.split(" ");
    for(int i;i<s.length;i++){
        System.out.println(s[i]);
    }

    (9)字符串的大小写转换

    System.out.println("hello".toUpperCase());
    System.out.println("HELLO".toLowerCase());

    (10)判断是否以指定的字符串开头或结尾

    String str1 = "HELLO";
    String str2 = "HELLO**";
    if(str1.startsWith("**")){
          Sytem.out.println("(**HELLO)以**开头");
    }
    if(str1.endsWith("**")){
          Sytem.out.println("(HELLO**)以**结尾");
    }

    (11)不区分大小写进行字符串比较

    String str1 = "HELLO";
    String str2 = "hello";
    System.out.println(""HELLO"equals"hello""+str1.equals(str2));
    System.out.println(""HELLO"equalsIgnoreCaseCase"hello""+str1.equalsIgnoreCase(str2));

    (12)将一个指定的字符串,替换成其它的字符串

    String str = "hello";
    String newStr = str.replaceAll("l","x");
    System.out.println(newStr);

    二、对象数组

    Person per[] = new Person[数字];
    per[0] = new Person("内容");                //实例化元素
    ..........

    实验报告(二)

    实验二 Java简单类与对象

    一、 实验目的

    (1)      掌握类的定义,熟悉属性、构造函数、方法的作用,掌握用类作为类型声明变量和方法返回值;

    (2)      理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性;

    (3)      理解static修饰付对类、类成员变量及类方法的影响。

    二、 实验内容

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

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

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

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

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

     

     

    一、Rectangle类

    1.实验源码

    package rectangle;
    
    
    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 void tell() {
            System.out.println("面积:"+getArea()+"
    "+"周长:"+getLength());
            System.out.println("颜色:"+getColor());
        }
        
        
        public String getColor() {
            return color;
        }
        public void setColor(String x) {
            color=x;
        }
        public double getWidth() {
            return width;
        }
        public void setWidth(double n) {
            width=n;
        }
        public double getHeight() {
            return height;
        }
        public void setHeight(double a) {
            height=a;
        }
        
        
        public double getArea(){
            double area=getWidth()*getHeight();
            return area;
            
        }
        public double getLength(){
            double length=2*getWidth()+2*getHeight();
            return length;
        }
    }
    
    
    
    
    
    package rectangle;
    
    public class Rectangle2 {
        public static void main(String[] args) {
            Rectangle tra=new Rectangle(10,30,"red");
            tra.tell();
        }
    
    }

    2.实验结果

    二、Account类

    1.实验源码

    package account;
    
    class Account {
        private String mark;
        private String name;
        private long day;
        private long password;
        private double balance;
        public Account(String mark,String name,long day,long password,double balance) {
            this.setMark(mark);
            this.setName(name);
            this.setDay(day);
            this.setPassword(password);
            this.setBalance(balance);
        }
        public Account(String mark,long password,double balance) {
            this.setMark(mark);
            this.setPassword(password);
            this.setBalance(balance);
        }
        
        public void tell() {
            System.out.println("标识:"+getMark()+"
    "+"姓名:"+getName()+"
    "+"开户日期:"+getDay()+"
    "+"初始密码:"+getPassword()+"
    "+"余额:"+getBalance());
        }
        public void tell2() {
            System.out.println("标识:"+getMark()+"
    "+"姓名:"+getName()+"
    "+"开户日期:"+getDay()+"
    "+"密码:"+getPassword()+"
    "+"余额:"+getBalance());
        }
        public String getMark() {
            return mark;
        }
        public void setMark(String m) {
            mark=m;
        }
        public String getName() {
            return name;
        }
        public void setName(String n) {
            name=n;
        }
        public long getDay() {
            return day;
        }
        public void setDay(long d) {
            day=d;
        }
        public long getPassword() {
            return password;
        }
        public void setPassword(long p) {
            password=p;
        }
        public double getBalance() {
            return balance;
        }
        public void setBalance(double b) {
            balance=b;
        }
    }
    package account;
    
    import java.util.*;
    import java.util.Scanner;
    public class Account1 {
        
        public static void main(String[] args) {
            double b=0.0d;
            Account acc=new Account("xh123456789",123456,0);
            acc.tell();
            Scanner sc = new Scanner(System.in);
             Scanner out = new Scanner(System.in);
             System.out.print("请输入开户姓名:");
             String n =null;
             n=sc.nextLine();
             
             System.out.print("请设置密码:");
             long p = out.nextLong();
             System.out.print("请输入日期:");
             long d = out.nextLong();
             System.out.println("
    ");
             Account acc1=new Account("xh123456789",n,d,p,b);
            acc1.tell2();
            System.out.println("
    ");
            int z=7;
            System.out.println("存款:1"+"	"+"取款:2"+"	"+"查询余额:3"+"
    "+"修改密码:4"+"	"+"查询标识:5"+"	"+"查询姓名:6");
            while(z!=0) {
             
              z = out.nextInt();
             if(z==1) {
             System.out.print("存入的金额:");
             double a = out.nextDouble();
             b=b+a;
             System.out.print("存入:"+a);
             }
             if(z==2) {
             System.out.print("取出的金额:");
             double r = out.nextDouble();
             b=b-r;
             System.out.print("取出:"+r);
             }
             if(z==3) {System.out.print("余额:"+b);}
             if(z==4) {
                 System.out.print("请修改密码:");
                  p = out.nextLong();
                  acc1=new Account("xh123456789",n,d,p,b);
                  acc1.tell2();
             }
             if(z==5) {
                 
                 System.out.println("标识:"+"xh123456789");
             }
             if(z==6) {
                 System.out.println("姓名:"+n);
                 
             }
            }
            
            
    
    
        }
    }

    2.实验结果

    三、总结

    输入需要加 import java.util.*;

    对于内容的输入的语句不太熟练,字符串的输入和数字的又不一样

    输入的基本语句:

    数据类型   a = out.next数据类型(首字母大写)();

    方法的调用也不熟练

    第二题写的怪怪的。。。。

  • 相关阅读:
    lintcode-144-交错正负数
    lintcode-143-排颜色 II
    lintcode-142-O(1)时间检测2的幂次
    lintcode-141-x的平方根
    lintcode-138-子数组之和
    postman-----使用CSV和Json文件实现批量接口测试
    postman—创建collection,执行collection和批量执行
    postman-变量
    postman-鉴权
    Postman—cookie
  • 原文地址:https://www.cnblogs.com/xu23/p/11550931.html
Copyright © 2020-2023  润新知