目录
- 第47题 填写代码. 类的基本操作简单例子(10分)
- 🍋题目描述
- 🍋源代码
- 第48题 填写代码. 关于Point类的操作(2)(10分)
- 🍋题目描述
- 🍋源代码
第47题 填写代码. 类的基本操作简单例子(10分)
🍋题目描述
下面程序是类的基本操作简单例子.
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java
public class Main {
public static void main (String args[ ]) {
【代码1】//命令行窗口输出"教学活动从教室开始"
Teacher zhang = new Teacher();
Student jiang = 【代码2】Student();//创建对象
zhang.introduceSelf();
jiang.【代码2】; //调用它的方法
}
}
class Teacher {
void introduceSelf() {
【代码3】 //命令行窗口输出"我是李老师"
}
}class Student {
void introduceSelf() {
【代码4】/ /命令行窗口输出"我是学生,名字是:奖励"
}
}
此题的上机步骤是:
- 建立一个Java项目,名称可以按题号取名;
- 建立一个类, 类的名称为Main。这一点非常重要;
- 填代码;
- 提交代码,注意题号要一致。
🍋源代码
public class Main {
public static void main (String args[ ]) {
System.out.println("教学活动从教室开始");//命令行窗口输出"教学活动从教室开始"
Teacher zhang = new Teacher();
Student jiang = new Student();//创建对象
zhang.introduceSelf();
jiang.introduceSelf(); //调用它的方法
}
}
class Teacher {
void introduceSelf() {
System.out.println("我是李老师"); //命令行窗口输出"我是李老师"
}
}
class Student {
void introduceSelf() {
System.out.println("我是学生,名字是:奖励");//命令行窗口输出"我是学生,名字是:奖励"
}
}
第48题 填写代码. 关于Point类的操作(2)(10分)
🍋题目描述
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java
public class Main {
public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println(“Point1:” + “(” +
Point1.x + “,” + Point1.y + “)”);
Point Point2 = Point1.getPoint();
System.out.println(“Point2:” + “(” +
Point2.x + “,” + Point2.y + “)”);
Point 【代码1】 = new Point(5, 6);
Point1.setPoint(Point3);
System.out.println(“Point1:” + “(” +
Point1.x + “,” + Point1.y + “)”);
}
}
class Point {
int x, y;
public Point(int x, 【代码2】) {
this.x = x;
this.y = y;
}
【代码3】 Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}
public void setPoint(Point point) {
this.x = point.x;
【代码4】y = point.y;
}
}
此题的上机步骤是:
- 建立一个Java项目,名称可以按题号取名;
- 建立一个类, 类的名称为Main。这一点非常重要;
- 填代码;
- 提交代码,注意题号要一致。
🍋源代码
public class Main {
public static void main(String[] args) {
Point Point1 = new Point(3, 4);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
Point Point2 = Point1.getPoint();
System.out.println("Point2:" + "(" +
Point2.x + "," + Point2.y + ")");
Point Point3 = new Point(5, 6);
Point1.setPoint(Point3);
System.out.println("Point1:" + "(" +
Point1.x + "," + Point1.y + ")");
}
}
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point getPoint() {
Point tempPoint = new Point(x, y);
return tempPoint;
}
public void setPoint(Point point) {
this.x = point.x;
this.y = point.y;
}
}