1. 编写Java代码实现一个计数器类“Counter”, 其中包含域“counterValue”用来保存计数器的当前数值、方法“increment()”使计数器加一、方法“decrement()”使计数器减一、方法“reset()”使计数器清零。构造计数器类的对象,并使用。
package project;
public class Counter {
private int counterValue;
public Counter() {
counterValue = 0;
}
public void increment() {
counterValue++;
}
public void decrement() {
counterValue--;
}
public void reset() {
counterValue = 0;
}
public int getCounterValue() {
return counterValue;
}
public static void main(String[] args) {
Counter counter = new Counter();
counter.increment();
System.out.println("计数器值: " + counter.getCounterValue());
counter.increment();
System.out.println("计数器值: " + counter.getCounterValue());
counter.decrement();
System.out.println("计数器值: " + counter.getCounterValue());
counter.reset();
System.out.println("计数器值: " + counter.getCounterValue());
}
}
2. 编写一个只含有一个方法“int method(int n)”的接口“InterfaceA”,再编写一个类“ClassA”来实现接口“InterfaceA”,实现接口方法“int method(int n)”时,要求计算1到n的和。声明接口的变量指向新构建“ClassA”的对象,使用接口方法测试“n = 4”时的程序执行情况。
InterfaceA.java
package project;
public interface InterfaceA {
int method(int n);
}
ClassA.java
package project;
public class ClassA implements InterfaceA {
public int method(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
public static void main(String[] args) {
InterfaceA a = new ClassA();
int n = 4;
System.out.println("1到" + n + "的和为: " + a.method(n));
}
}
3. 在上题的基础上,编写另一个类“ClassB”来实现接口“InterfaceA”,实现接口方法“int method(int n)”时,要求计算“n”的阶乘“n!”。声明接口的变量指向新构建“ClassB”的对象,使用接口方法测试“n = 4”时的程序执行情况。
package project;
public class ClassB implements InterfaceA {
public int method(int n) {
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
public static void main(String[] args) {
InterfaceA b = new ClassB();
int n = 4;
System.out.println(n + "的阶乘为: " + b.method(n));
}
}
4. 编程实现矩形类,其中要包括计算矩形周长和面积的方法。
package project;
public class Rectangle {
// 定义两个成员变量记录矩形长和宽
private double length;
private double width;
// 构造函数
public Rectangle(double l, double w) {
length = l;
width = w;
}
// 计算矩形周长
public double perimeter() {
return 2 * (length + width);
}
// 计算矩形面积
public double area() {
return length * width;
}
public static void main(String[] args) {
// 创建一个矩形对象,长为5,宽为3
Rectangle rect = new Rectangle(5, 3);
// 计算矩形周长和面积
double peri = rect.perimeter();
double ar = rect.area();
System.out.println("矩形周长为:" + peri);
System.out.println("矩形面积为:" + ar);
}
}
5. 使用上题的矩形类,编程统计若干块土地的相关信息,由用户输入每块儿土地的长与宽,程序将相关结果输出。
package project;
import java.util.Scanner;
public class LandCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入土地的块数:");
int count = input.nextInt();
Rectangle[] lands = new Rectangle[count];
double totalArea = 0;
double totalPerimeter = 0;
for (int i = 0; i < count; i++) {
System.out.print("请输入第" + (i + 1) + "块土地的长和宽:");
double length = input.nextDouble();
double width = input.nextDouble();
lands[i] = new Rectangle(length, width);
System.out.println("面积为:" + lands[i].area());
System.out.println("周长为:" + lands[i].perimeter());
}
}
}
6. 定义一个表示学生的类“Student”,包括属性学号“id”、姓名“name”、性别“gender”、班级编号“classId”、年龄“age”;包括方法“getId()”、“getName()”、“getGender()”、“getClassId()”、“getAge()”、“setAge(int age)”。
package project;
public class Student {
private String id;
private String name;
private String gender;
private String classId;
private int age;
// 构造函数
public Student(String id, String name, String gender, String classId, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.classId = classId;
this.age = age;
}
// 获取学生学号
public String getId() {
return id;
}
// 获取学生姓名
public String getName() {
return name;
}
// 获取学生性别
public String getGender() {
return gender;
}
// 获取学生班级编号
public String getClassId() {
return classId;
}
// 获取学生年龄
public int getAge() {
return age;
}
// 设置学生年龄
public void setAge(int age) {
this.age = age;
}
}
7. 在上题的基础上为“Student”类定义构造方法并初始化所有的属性,增加一个 “toString()”方法把“Student”类对象的所有属性信息组合成一个字符串。编程创建“Student”类的对象并测试新增的toString功能。
Student.java中添加
// 返回包含所有属性信息的字符串
public String toString() {
return "学号:" + id + "\n姓名:" + name + "\n性别:" + gender + "\n班级编号:" + classId + "\n年龄:" + age;
}
StudentTest.java
package project;
public class StudentTest {
public static void main(String[] args) {
Student student = new Student("001", "张三", "男", "ClassA", 18); System.out.println(student.toString());
}
}
8. 定义一个父类“Animal”,包含public成员属性“名字”、“年龄”与方法“showProfile()”。方法“showProfile()”,把对象的所有属性信息组合成一个字符串。接着,定义其子类“Cat”并包含一个成员方法“sleep()”,模拟睡觉行为。编程创建“Cat”类的对象,为该对象的“名字”、“年龄”属性赋值,并调用父类中定义的方法“showProfile()”与子类中定义的方法“sleep()”。
Animal.java
package project;
class Animal {
public String name;
public int age;
public String showProfile() {
return "姓名:" + name + "\n年龄:" + age;
}
}
Cat.java
package project;
class Cat extends Animal {
public void sleep() {
System.out.println(name + " is sleeping.");
}
}
CatTest.java
package project;
public class CatTest {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "Tom";
cat.age = 2;
System.out.println(cat.showProfile());
cat.sleep();
}
}
9. 在上题的基础上,定义一个“Animal”的子类“Dog”,包含成员方法“run()” ,模拟奔跑行为。编程分别创建一个“Cat”类的对象和一个“Dog”类的对象并分别为两个对象的属性赋值,分别调用它们的“showProfile()”方法,然后调用各对象特有的方法“sleep()”和“run()”。
Dog.java
package project;
class Dog extends Animal {
public void run() {
System.out.println(name + " is running.");
}
}
DogTest.java
package project;
public class DogTest {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "Tom";
cat.age = 2;
Dog dog = new Dog();
dog.name = "Lisa";
dog.age = 3;
System.out.println(cat.showProfile());
cat.sleep();
System.out.println(dog.showProfile());
dog.run();
}
}
10. 在上题的基础上为“Animal”类增加成员方法“speak()”,模拟发声行为,并在子类“Cat”与“Dog”中分别覆盖重写该方法,编程创建两个子类的对象并调用“showProfile()”及“speak()”方法。
Animal.java中添加
public void speak() {
System.out.println("Animal is making a sound.");
}
Cat.java中添加
public void speak() {
System.out.println(name + " says Meow!");
}
Dog.java中添加
public void speak() {
System.out.println(name + " says Woof!");
}
SpeakTest.java
package project;
public class SpeakTest {
public static void main(String[] args) {
Cat cat = new Cat();
cat.name = "Tom";
cat.age = 2;
Dog dog = new Dog();
dog.name = "Lisa";
dog.age = 3;
System.out.println(cat.showProfile());
cat.speak();
System.out.println(dog.showProfile());
dog.speak();
}
}
11. 在上题的基础上编程创建一个长度为4的“Animal”类的数组,并创建该数组中的偶数元素为“Cat”类的对象,奇数元素为“Dog”类的对象,调用数组中所有对象的“speak()”方法。
package project;
public class SpeakAnimal {
public static void main(String[] args) {
Animal[] animals = new Animal[4];
for (int i = 0; i < animals.length; i++) {
if (i % 2 == 0) {
animals[i] = new Cat();
animals[i].name = "Cat " + i;
} else {
animals[i] = new Dog();
animals[i].name = "Dog " + i;
}
}
for (Animal animal : animals) {
animal.speak();
}
}
}
12. 设计一个名为MyPoint的类,包含以x坐标和y坐标表示的点。具体的,设计该类的需求包括:
数据属性x和y,用于表示坐标,并均有设置(setter)和获取(getter)的方法;
创建点(0,0)的无参构造方法;
创建特定坐标点的构造方法;
distance方法,返回从该点到MyPoint类型的指定点之间的距离;
distance的方法,返回从该点到由x和y确定的点之间的距离,体会方法重载;
编写一个测试程序,创建两个点的对象,并显示他们之间的距离。
MyPoint.java
package project;
import java.lang.Math;
public class MyPoint {
private double x;
private double y;
// 创建点(0,0)的无参构造方法
public MyPoint() {
this.x = 0;
this.y = 0;
}
// 创建特定坐标点的构造方法
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
// 设置X轴坐标
public void setX(double x) {
this.x = x;
}
// 获取X轴坐标
public double getX() {
return x;
}
// 设置Y轴坐标
public void setY(double y) {
this.y = y;
}
// 获取Y轴坐标
public double getY() {
return y;
}
// 从该点到MyPoint类型的指定点之间的距离
public double distance(MyPoint point) {
double distance = Math.sqrt(Math.pow(this.x - point.getX(), 2) + Math.pow(this.y - point.getY(), 2));
return distance;
}
// 从该点到由x和y确定的点之间的距离
public double distance(double x, double y) {
double distance = Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
return distance;
}
}
MyPointTest.java
package project;
public class MyPointTest {
public static void main(String[] args) {
MyPoint point1 = new MyPoint(3, 4);
MyPoint point2 = new MyPoint(6, 8);
double distance = point1.distance(point2);
System.out.println("Distance between point1 and point2: " + distance);
distance = point1.distance(7, 7);
System.out.println("Distance between point1 and (7,7): " + distance);
}
}
标签:Java,String,int,double,System,实验,面向对象编程,public,out
From: https://blog.csdn.net/weixin_69276268/article/details/143368900