Java_实验二

(一)学习总结
1、构造方法用于在创建对象时对其进行初始化Person p=new Person
他的特点:(1)方法名与类名相同(2)方法名前没有返回值类的声明,方法中不能使用return语句返回一个值(3)用户不能直接调用,而是通过关键字new自动调用
方法的重载就是方法名称相同,但参数的类型和参数的个数不同,通过传递参数的个数及类型不同以完成不同功能的方法调用。

public class Test {
    public static void main(String args[]) { 
       Foo obj = new Foo();       
    }     
}
class Foo{
    int value;
    public Foo(int intValue){
        value = intValue;
    }
}

不可以,类中没有定义无参的构造方法

2、编译没有错误,在运行时出错

public class Test {
    public static void main(String[] args) {
        MyClass[] arr=new MyClass[3];
        arr[1].setValue(100);
    }
}
class MyClass{
    public int value=1;
    public MyClass(){
        
    }
    public MyClass(int value){
        this.value=value;
    }
    public int getValue(){
        return value;
    }
    public void setValue(int value){
        this.value=value;
    }
}

3、

public class Test {
    public static void main(String[] args) {
        Foo obj1 = new Foo();
        Foo obj2 = new Foo();
        System.out.println(obj1 == obj2);
    }
}
class Foo{
    int value = 100;
}

运行结果为false ==比较的是地址,应该用equals方法
4、

class CashCard {
    String number;
    int balance;
    int bonus;
    public CashCard(String number,int balance,int bonus){
        number = number;
        balance = balance;
        bonus = bonus;
    }
}
public class Test {
    public static void main(String[] args) {
        CashCard card1 = new CashCard("A001",500,0);
        CashCard card2 = new CashCard("A002",300,0);
        CashCard card3 = new CashCard("A003",1000,1);
    }
}

封装把对象的所有组成部分(数据和方法)封装在一起构成类,对象本身的数据的得到保护/隐藏,其他对象通过该对象的访问方法(接口/interface)与之发生联系,好处:模块化,信息隐藏—通常定义一个公共接口/方法实现对对象的访问

5、(1)

class A{
    private int secret = 5;
}
public class Test{
    public static void main(String args[]){
        A a = new A();
        System.out.println(a.secret++);
    }
}

不能通过编译,在类中没有定义secret的getter和setter方法

(2)

public class Test{
    int x = 50;
    static int y = 200;
    public static void method(){
        System.out.println(x+y);
    }
    public static void main(String args[]){
        Test.method();
    }
}

不能通过编译,因为x不是静态变量
6、使用类的静态变量和构造方法,可以跟踪某个类创建的对象个数。声明一个图书类,数据成员为编号,书名,书价,并拥有静态数据成员册数记录图书的总数。图书编号从1000开始,每产生一个对象,则编号自动递增(利用静态变量和构造方法实现)。


class Book{
    int bookId;
    String bookName;
    double price;
    String str;
    static int num;
    static{
        num=1000;
    }
    Book(){
        
    }
    Book(String bookName,double price){
        this.bookName=bookName;
        this.price=price;
        num++;
    }
     public String getBookName() {
        return bookName;
    }
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }  
    public static int totalBook(){
        return num-1000;
    }
    public void tostring(){
        System.out.println("书名:"+bookName+" 书价:"+price);
    }
}
public class Test{
    public static void main(String args[]){ 
        Book[] books = {new Book("c语言程序设计",29.3),
                        new Book("数据库原理",30),
                        new Book("Java学习笔记",68)};
        System.out.println("图书总数为:"+ Book.totalBook()); 
        for(Book book:books){
            book.tostring();
        }
    }   
}

7、
在设计一个类时,需要保证在整个程序运行期间针对该类只存在一个实例对象,构造方法私有化,在类内创建一个实例对象,用私有静态成员instance引用该对象,定义静态方法getInstance()返回实例对象instance

class Sun{
    private static Sun instance = new Sun() ;
    private Sun(){   }
    public static Sun getInstance(){
        return instance ;
    }
}
public class Test{
    public static void main(String args[]){
        Sun s1 = Sun.getInstance() ;
        Sun s2 = Sun.getInstance() ;
        System.out.println(s1==s2);
    }
}

8、

public class Test {
    String str = new String("你好  ");
    char[] ch = { 'w','o','l','l','d' };
    public static void main(String args[]) {
        Test test = new Test();
        test.change(test.str, test.ch);
        System.out.print(test.str);
        System.out.print(test.ch);
    }
    public void change(String str, char ch[]) {
        str = "hello";
        ch[0] = 'W';
    }
}

运行结果为:你好Wolld

(二)实验总结
1.用面向对象思想完成评分系统
程序设计思路:
设计一个选手类,一个测试类,一个平分类写出他们的构造方法以及getter和setter方法,构造方法(有参和无参),在评分类中计算平均分,在测试类中进行验证
问题以及解决方案:
(1)、开始没有写无参构造方法,在调用时出错
(2)、注意","和“.”的使用,在编写程序时注意不要打错
(3)、输入的评委人数一定要大于三否则去掉最高和最低没有成绩
(4)、注意循环的使用以及嵌套,不要重复输入没有必要的信息

2.Email验证
程序设计思路:
可以设计4个方法分别测试email的地址是否合法,注意@和。同时存在,可用查询如果没有返回-1,查看是否同时存在,对于@在。之前可以检测他们的下标的大小,对于不能以@开头可以看看下表为0的是否为@,对com或cn或net或gov或edu或org结尾的可以提取字符串的后三个字符进行比较,在主方法中判断他们是否都正确
问题以及解决方案:
(1)、对cn的测试可以用。cn
(2)、注意字符串方法的调用为str.indexOf();
(3)、可以使用Boolean类型的返回值,在主方法中进行验证

3.查找子串
程序设计思路:
定义一个方法找到字符串中有几个子串,返回子串的个数,主方法中输出,在while循环中调用indexof方法,注意从方法返回的下标继续查找
问题以及解决方案:
(1)、注意是从indexof方法返回的下标开始第二次的查询,以后同理
(2)、注意当返回的下标不为一时,子串个数加一

4.统计文件
程序设计思路:
可以编写一个方法查找有多少个文件类型一个数,且进行文件首字母大写输出,先用split方法对字符串进行分离,再用substring进行文件第一个字母分离,大写输出,在定义字符串数组按“.”分离字符串,把字符串数组的最后一个字符串放到一个新的字符串数组中,计算文件类型以及个数
问题以及解决方案:
(1)、在对文件的首字母大写输出是可以用str.substring(0,1).toUpperCase()
(2)、在计数文件的个数时注意数组不要越界
(3)、在进行文件类型的统计要考虑到以a。txt为文件名的情况

5.类的设计
程序设计思路:
设计一个日期类,一个职工类,一个部门类,分别写出他们的getter,setterfangfa以及含参和不含参的构造方法,日期类可以调用str。replaceAll(",","-")方法,在测试类中调用getter和setter方法对职工的生日和参加工作时间进行设置,在测试类中进行信息的设置,实现查询
问题以及解决方案:
(1)、在设置日期类时可以调用replaceAll()方法
(2)、在改变员工的日期类形时,要调用getter和setter方法
(3)、可以对信息进行初始化在查询
(4)、可以设置一个Boolean类型的变量,如果不想继续查询可以终止查询

1032134-20180401221356547-983334119.png

转载于:https://www.cnblogs.com/lr97/p/8684495.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基本的Java实验,用于实现复数类和一些基本操作: ```java public class ComplexNumber { private double real; private double imaginary; public ComplexNumber(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public double getReal() { return real; } public double getImaginary() { return imaginary; } public ComplexNumber add(ComplexNumber other) { return new ComplexNumber(real + other.real, imaginary + other.imaginary); } public ComplexNumber subtract(ComplexNumber other) { return new ComplexNumber(real - other.real, imaginary - other.imaginary); } public ComplexNumber multiply(ComplexNumber other) { double newReal = real * other.real - imaginary * other.imaginary; double newImaginary = real * other.imaginary + imaginary * other.real; return new ComplexNumber(newReal, newImaginary); } public ComplexNumber divide(ComplexNumber other) { double denominator = other.real * other.real + other.imaginary * other.imaginary; double newReal = (real * other.real + imaginary * other.imaginary) / denominator; double newImaginary = (imaginary * other.real - real * other.imaginary) / denominator; return new ComplexNumber(newReal, newImaginary); } public String toString() { if (imaginary < 0) { return real + " - " + (-imaginary) + "i"; } else { return real + " + " + imaginary + "i"; } } } ``` 这个类定义了一个名为ComplexNumber的复数类,包含实部和虚部。您可以通过调用实例方法来执行以下操作: - add():添加两个复数。 - subtract():从两个复数中减去一个。 - multiply():将两个复数相乘。 - divide():将两个复数相除。 - toString():将复数作为字符串返回。 以下是一个简单的示例,演示如何使用ComplexNumber类: ```java public class Main { public static void main(String[] args) { ComplexNumber c1 = new ComplexNumber(1, 2); ComplexNumber c2 = new ComplexNumber(3, 4); ComplexNumber sum = c1.add(c2); ComplexNumber difference = c1.subtract(c2); ComplexNumber product = c1.multiply(c2); ComplexNumber quotient = c1.divide(c2); System.out.println("c1: " + c1.toString()); System.out.println("c2: " + c2.toString()); System.out.println("Sum: " + sum.toString()); System.out.println("Difference: " + difference.toString()); System.out.println("Product: " + product.toString()); System.out.println("Quotient: " + quotient.toString()); } } ``` 输出结果: ``` c1: 1.0 + 2.0i c2: 3.0 + 4.0i Sum: 4.0 + 6.0i Difference: -2.0 - 2.0i Product: -5.0 + 10.0i Quotient: 0.44 + 0.08i ``` 希望这个例子能够对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值