实验内容及步骤:
(1)代码: class Student { String name="张三"; int age=20; String sex="男";//gender String getName(){ return name; } void setName(String newName) { name=newName; } void read() { System.out.println("我是"+name+"年龄:"+age+",我正在学习!"); } void c(Student student) { student.name="王琳"; student.age=200; student.sex="male"; } public static void main(String[] args) { Student student=null; student=new Student(); System.out.println(student); student.read(); Student student1=new Student(); 实验内容及步骤: System.out.println(student1); student1.read(); Student student2=student; System.out.println(student2); } } Test: public class Test { public static void main(String[] args) { Student student=new Student(); Student s=new Student(); student.name="李四"; student.setName("李四"); student.age=22; student.sex="女"; System.out.println(student.getName()); student.read(); student.c(student); student.read(); s.name="张三"; s.age=100; s.sex="female"; student=s; student.read(); s.read(); } } (2)运行结果:
(1)代码: 实验内容及步骤: public class Circle { private double radius; // 构造函数 public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public double getDiameter() { return 2 * radius; } public double getCircumference() { return 2 * Math.PI * radius; } public double getArea() { return Math.PI * radius * radius; } } Test: public class Test { public static void main(String[] args) { Circle circle = new Circle(5.0); System.out.println("圆的半径:" + circle.getRadius()); System.out.println("圆的直径:" + circle.getDiameter()); System.out.println("圆的周长:" + circle.getCircumference()); System.out.println("圆的面积:" + circle.getArea()); } } (2)运行结果: 实验内容及步骤:
(1)代码: public class MathUtils { public static int findMax(int[] numbers) { if (numbers == null || numbers.length == 0) { throw new IllegalArgumentException("数组不能为空"); } int max = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } } return max; } public static int sum(int[] numbers) { if (numbers == null || numbers.length == 0) { throw new IllegalArgumentException("数组不能为空"); } int sum = 0; for (int number : numbers) { sum += number; 实验内容及步骤: } return sum; } } Test: public class Test { public static void main(String[] args) { int[] numbers = {2, 5, 1, 8, 4}; int maxValue = MathUtils.findMax(numbers); System.out.println("最大值:" + maxValue); int sumValue = MathUtils.sum(numbers); System.out.println("求和结果:" + sumValue); } }
4、编写对包操作的程序。(学生类及测试类分别放到两个包里) (1)代码: public class Student | |
实验内容及步骤: return age; } Test: public class Test { (2)运行结果: | |
你的问题:
|