JAVA实训第三天课堂记录

一.一维数组
数据类型 变量名 = 初始值;

数据类型[] 数组名 = new 数据类型[数组的长度] ; 动态声明方式

数据类型[] 数组名 = new 数据类型[]{数组元素};静态声明方式

1.数组的声明 初始值
public class ArrayDemo01 {
    public static void main(String[] args) {
        //声明数组
        int[] arr = new int[5];
        System.out.println(arr[0]);
        boolean[] brr = new boolean[5];
        System.out.println(brr[0]);
        char[] crr = new char[5];
        if (crr[0]==0){
            System.out.println("char数组的默认值为0。");
        }
        System.out.println("------");
 
    }
}


 2.数组的输出遍历
public class ArrayDemo02 {
    public static void main(String[] args) {
        //声明数组
        int[] arr = new int[]{1,2,3,4,5};
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
        System.out.println(arr[3]);
        System.out.println(arr[4]);
        //System.out.println(arr[5]);ArrayIndexOutOfBoundsException: 5
        //数组的遍历
        int[] arr1 = new int[20];
        for(int i=0;i< arr1.length;i++){
            System.out.print(arr1[i]+" ");
        }
    }
}

 3.双色球案例

import java.util.Random;
 
 
public class ArrayDemo03 {
    public static void main(String[] args) {
        //双色球 共七个号码,其中红球6个,范围1-33,篮球1个范围1-17
        //声明一个int类型长度为七的一维数组,动态
        int[] arr = new int[7];
        //创建一个随机数对象
        Random random = new Random();
        //摇号 向数组当中添加值,需要先摇6个红球 范围1-33
        for (int i = 0; i < arr.length - 1; i++) {
            arr[i] = random.nextInt(33) + 1;
            //去重
            for (int j = i - 1; j >= 0; j--){
                if(arr[i]==arr[j]){
                    //若号码出现重复,需重新摇号
                    i--;
                    break;
                }
            }
        }
        //蓝球
        arr[arr.length - 1] = random.nextInt(17) + 1;
        //将数组中的双色球遍历
        System.out.println("本期中奖结果:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
 
    }
}
 

二.面向对象
1.类、对象、方法
(1)定义一个坐标类并修改坐标
public class Point {
    int x;
    int y;
 
    public static void main(String[] args) {
        //创建一个Point类型的引用point,指向Point类型的对象
        Point point = new Point();
        System.out.println("x = " + point.x);
        System.out.println("y = " + point.y);
        point.x = 3;
        point.y = 4;
        System.out.println("x = " + point.x);
        System.out.println("y = " + point.y);
    }
}


 (2)定义手机类包括品牌和价格并修改品牌和价格
public class Phone {
    String brand;
    double price;
 
    public void show() {
        System.out.println("品牌:" + brand + " 价格:" + price);
    }
 
    public static void main(String[] args) {
        Phone p = new Phone();
        p.show();
        p.brand = "Nokia";
        p.price = 598.5;
        p.show();
    }
}

 (3)定义人类包括姓名和年龄并修改姓名和年龄
public class Person {
    String name;
    int age;
 
    public void show() {
        System.out.println("姓名:" + name + " 年龄:" + age);
    }
 
    public static void main(String[] args) {
        Person p = new Person();
        p.show();
        p.name = "zhangfei";
        p.age = 30;
        p.show();
 
    }
}

 (4)加法(既有返回值又有形参)
public class MethodDemo01 {
    public int add(int a, int b) {
        return a + b;
    }
 
    public static void main(String[] args) {
        MethodDemo01 m = new MethodDemo01();
        int sum = m.add(1, 2);
        System.out.println("sum = " + sum);
 
    }
 
}


 (5)创建一个女孩类包括姓名、年龄、是否有男朋友 以及成员变量的获取和修改
public class Girl {
    String name;
    int age;
    boolean bf;
 
    public void show() {
        System.out.println("姓名:" + name + " 年龄:" + age + " 是否有男朋友:" + bf);
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public boolean isBf() {
        return bf;
    }
 
    public void setBf(boolean bf) {
        this.bf = bf;
    }
 
    public static void main(String[] args) {
        Girl g = new Girl();
        g.show();
        g.name = "貂蝉";
        g.age = 18;
        g.bf = true;
        g.show();
    }
 
 
}

 2.构造方法和方法重载
构造方法:[权限修饰符] 构造方法名 [类名](形参列表){构造方法体}

方法重载:名称相同 参数列表不同(参数个数,参数类型。参数顺序)

(1)

public class Point {
    int x;
    int y;
 
    public Point() {
 
    }
 
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
 
    public String show() {
        return "x = " + x + " y = " + y;
 
    }
 
    public int addx() {
        return x + 1;
    }
 
    public int addy(int l) {
        return y + l;
    }
 
    public static void main(String[] args) {
        Point point = new Point();
        point.x = 3;
        point.y = 4;
        System.out.println("x = " + point.x);
        System.out.println("y = " + point.y);
        Point point1 = new Point(3, 4);
        System.out.println(point1.show());
        System.out.println("横坐标加一:" + point.addx());
        System.out.println("纵坐标增长2:"+point.addy(2));
    }
}

 (2)

public class Circle {
    double r;
    final double PI = 3.14;
 
    public double getArea() {
        return PI * r * r;
    }
 
    public double getPerimeter() {
        return 2 * PI * r;
    }
 
    public Circle(double r) {
        this.r = r;
    }
 
    public static void main(String[] args) {
        Circle circle = new Circle(3);
        System.out.println("面积为:"+circle.getArea());
        System.out.println("周长为:"+circle.getPerimeter() );
    }
}

 (3)

public class Rectangle {
    double length;
    double width;
 
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
 
    public double getArea() {
        return length * width;
    }
 
    public double getPerimeter() {
        return 2 * (length + width);
    }
 
    public String showAll() {
        return "长:" + length + " 宽:" + width + " 面积:" +getArea()+ " 周长:"+getPerimeter();
    }
 
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(3,4);
        System.out.println(rectangle.showAll());
    }
}

 (4)

public class Car {
    String brand;
    String color;
    double price;
 
    public Car() {
 
    }
 
    public Car(String brand, String color, double price) {
        this.brand = brand;
        this.color = color;
        this.price = price;
    }
 
    public String show() {
        return "品牌:" + brand + " 颜色:" + color + " 价格:" + price;
 
    }
 
    public String getBrand() {
        return brand;
    }
 
    public void setBrand(String brand) {
        this.brand = brand;
    }
 
    public String getColor() {
        return color;
    }
 
    public void setColor(String color) {
        this.color = color;
    }
 
    public double getPrice() {
        return price;
    }
 
    public void setPrice(double price) {
        this.price = price;
    }
 
    public double addPrice1000() {
        return price + 1000;
    }
 
    public double addPrice(double p) {
        return price + p;
    }
 
    public static void main(String[] args) {
        Car car = new Car();
        car.brand="奔驰";
        car.color="黑色";
        car.price=500000;
        System.out.println(car.show());
        Car car1 = new Car("宝马","白色",500000);
        System.out.println(car1.show());
        System.out.println("宝马价格增长1000为:"+car1.addPrice1000());
        System.out.println("宝马价格增长5000为:"+car1.addPrice(5000));
    }
}

3.封装
(1)私有化成员变量,使用private关键字修饰

(2)提供公有的get(),set()方法并在方法体中进行合理值的判断

(3)在构造方法中调用set()方法进行合理值判断

public class Person {
    //私有化成员变量
    private String name;
    private int age;
 
    public Person() {
    }
 
    public Person(String name, int age) {
        this.name = name;
        setAge(age);
    }
 
    public void show() {
        System.out.println("姓名:" + name + " 年龄:" + age);
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
 
    public void setAge(int age) {
        if (age > 0 && age < 150) {
            this.age = age;
        } else {
            this.age = 18;
            System.out.println("你输入的年龄有误!");
        }
    }
}
 

import org.junit.Test;
 
import static org.junit.Assert.*;
 
public class PersonTest {
    @Test
    public void test01() {
        /*Person person = new Person();
        person.show();
        person.setName("张飞");
        person.setAge(-1);
        person.show();*/
        Person person = new Person("ggg",-1);
        person.show();
    }
 
 
 
 
}

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/2201_75588545/article/details/135813007

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值