package day3; 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]; System.out.println(crr[0]); if (crr[0] == 0){ System.out.println("char数组的默认值为0"); } System.out.println("--------------"); } }
package day3; public class ArrayDemo02 { public static void main(String[] args) { //声明数组 int[] arr = new int[]{1,2,3,4,5};//静态数组 int[] arr2 = new int[20];//动态数组 /*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 */ //数组的遍历 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
package day3; /** * 4.自定义Car类,特征有:品牌(brand)、颜色(color)、价格(price),行为有:无参构造、三个参数的构造、打印所有 * 要求在main()方法中使用无参形式构造对象打印特征,再使用有参形式构造对象打印特征, * 并使用有参形式构造的对象调用其他方法测试。 */ 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 void print(){ System.out.println("品牌:"+brand+",颜色:"+color+",价格:"+price); } public void print(String brand, String color, double price){ System.out.println("品牌:"+brand+",颜色:"+color+",价格:"+price); } public static void main(String[] args) { Car c = new Car(); c.print(); c.print("大众","白色",100000); } }
package day3; public class Circle { double r; public void setR(double r) { this.r = r; } public Circle(double r) { this.r = r; } public double getArea(){ double s = Math.PI*r*r; return s; } public double getRer(){ double c = 2*r*Math.PI; return c; } public void showAll(){ System.out.println("半径:"+r+",面积:"+getArea()+",周长:"+getRer()); } public static void main(String[] args) { Circle c = new Circle(3); c.getArea(); c.getRer(); c.showAll(); } }
package day3; import java.util.Random; /** * 3.定义一个矩形类Rectangle: * 定义三个方法: getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 * 有2个属性:长length、宽width * 通过构造方法Rectangle(int width, int length),分别给两个属性赋值创建一个Rectangle对象,并输出相关信息 */ public class Rectangle { int length; int width; int s; int c; public Rectangle(int length, int width) { this.length = length; this.width = width; } public int getArea(){ s = length*width; return s; } public int getPer(){ c = 2*(length+width); return c; } public void showAll(){ System.out.println("长:"+length+",宽:"+width+",面积:"+s+",周长:"+c); } public static void main(String[] args) { Rectangle r = new Rectangle(5,6); r.getArea(); r.getPer(); r.showAll(); } }
package day3; //方法重载 //参数个数,顺序,类型不同 public class Demo01 { public static void main(String[] args) { int a = 1; String name = "嘿嘿"; char c = 'a'; boolean b = true; System.out.println(a);//也属于重载 System.out.println(b); System.out.println(c); System.out.println(name); } }
package day3; import java.util.Random; /** * 练习 * 双色球案例 * 共7个号码,其中红球6个,范围1-33,篮球1个 范围是1-17 */ public class DoubleColorBall { public static void main(String[] args) { //声明一个int类型 长度为7的一维数组,动态 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;//1-33,从0开始33个 //去重 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]+" "); } } }
package day3; /** * *编程实现Girl类的定义,特征有:姓名、年龄、是否有男朋友bf,行为有:打印所有特征的方法, * *要求在main方法中声明GirL类型的引用指向GirL类型的对象并打印特征, * 然后修改特征为"貂蝉"、*18以及true后再次打印。 * * *练习2: * * 自定义成员方法实现将姓名修改为参数指定的数值自定义成员方法实现将年龄修改为参数指定的数值 * * 自定义成员方法实现将是否有男朋友修改为参数指定的数值自定义成员方法实现姓名的获取并返回 * * 自定义成员方法实现年龄的获取并返回 * * 自定义成员方法实现是否有男朋友的获取并返回 */ public class Girl { String name; int age; boolean aBoolean; public void show(){ System.out.println("姓名:" + name + ",年龄:" + age+ ",是否有对象:" + aBoolean); } public static void main(String[] args) { Girl girl = new Girl(); girl.show(); girl.name = "貂蝉"; girl.age = 18; girl.aBoolean = true; girl.show(); } //Alt+ins 快捷构造 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 isaBoolean() { return aBoolean; } public void setaBoolean(boolean aBoolean) { this.aBoolean = aBoolean; } }
package day3; public class MethDemo01 { public int add(int a,int b){ return a+b; } public static void main(String[] args) { MethDemo01 m = new MethDemo01(); int sum = m.add(1,2); System.out.println("sum = " + sum); } }
package day3; //实体类, public class Person { private String name; private int age; public void show(){ System.out.println("姓名:" + name + ",年龄:" + age); } //单元测试 public Person() { } public Person(String name, int age) { this.name = name; this.age = 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{ System.out.println("您输入的年龄有误!"); this.age = 18; } } }
package day3; /** * 编程是实现Person类的定义,特征有姓名和年龄 * 要求在main方法声明Person类型的引用指向Person类型的对象并打印特征, * 再将姓名和年龄修改为“zhangfei"和30后再次打印 */ public class Person01 { String name; int age; //当用户没有写任何形式的构造方法时,系统会自动的为程序提供一个无参构造方法 public Person01(){ System.out.println("我是无参构造。。正在被调用"); } //如果用户自己编写了构造方法,编译器不会再提供任何构造方法 // public Person(String name,int age){ // System.out.println("有参构造。。。"); // }Person person = new Person();//报错,原因:没有提供无参构造 public void show(){ System.out.println("姓名:" + name + ",年龄:" + age); } //是谁在调用main方法:JVM Java虚拟机 public static void main(String[] args) { Person01 person = new Person01(); person.show(); person.name = "zhangfei"; person.age = 30; person.show(); } }
package day3; import org.junit.Test; import static org.junit.Assert.*; public class PersonTest { @Test public void test01(){ Person person = new Person(); person.show(); // person.name = "张飞"; // person.age = 18;//报错,private person.setName("张飞"); person.setAge(-1); person.show(); Person person1 = new Person("嘿嘿",18); person1.show(); } @Test public void test02(){ Phone phone = new Phone(); phone.show(); } }
package day3; /** * 自定义phone类,特征有:品牌和价格,行为有:打印品牌和价格的方法 * 要求在main()方法声明Phone类型的引用指向Phone类型的对象并打印特征 * 将品牌和价格修改为“Nokia”和598.5后再次打印 */ 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(); } }
package day3; /** * 定义一个坐标类,特征有横坐标x和纵坐标y * 创建一个坐标类对象,将对象的信息打印出来,修改特征信息,并再次打印 */ 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); } }
package day3; /** * 练习:编程实现Point类,特征有:横坐标和纵坐标(整数),行为有:无参构造方法、有参构造方法、打印所有特征的方法 * 要求; * 在main方法中分别使用无参方式和有参方式构造对象并打印特征自定义成员方法实现横坐标增加1的行为 * 自定义成员方法实现纵坐标增长参数指定数值的行为 */ public class Point02 { int x; int y; public Point02() { } public Point02(int x, int y) { this.x = x; this.y = y; } public void print(){ System.out.println("x = " + x + "y = " + y); } public static void main(String[] args) { //创建一个Point类型的引用point,指向Point类型的对象 Point02 p = new Point02(); Point02 p2 = new Point02(1,1); p.print(); } }
package day3; /** * 2.请定义一个交通工具(vehicle)的类,其中有: * 属性:速度(speed),车的类型(type)等等 * 方法:移动(move()),设置速度(setSpeed(double s)),加速speedUp(double s),减速speedDown(double s)等等 * 最后在测试类vehicle中的main()中实例化一个交通工具对象,并通过构造方法给它初始化speed,type的值,并且打印出 */ public class Vehicle { double speed; String type; public Vehicle(double speed, String type) { this.speed = speed; this.type = type; } public void setSpeed(double speed) { this.speed = speed; } public void speedUp(double speed) { this.speed = speed; } public void speedDown(double speed) { this.speed = speed; } public void move(){ System.out.println("开始启动"); System.out.println("速度:"+speed+" 类型:"+type); } public static void main(String[] args) { Vehicle v = new Vehicle(10,"长安"); v.move(); } }