和之前一样,依旧以题为例为大家讲述java类的设计和对象的创建、成员变量和成员方法的设计,欢迎大家交流讨论、
1、Point类例子(1)
请将下面程序的【代码】替换为Java程序代码,使程序运行正确。
文件Main.java
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Point pt = 【代码1】 Point(1, n);// 创建对象
System.out.println("pt.x=" + 【代码2】.x);// 取
pt.x = 5;// 修改
System.out.println("pt.x=" + pt.x);
pt.move(3, 3);// 移动
System.out.println("pt.x=" + pt.x);
System.out.println("pt.y=" + pt.y);
【代码3】 pt2 = new Point();// 声明对象并new
System.out.println("pt2.x=" + pt2.x);
pt2 = new Point(9, 2);
System.out.println("pt2.x=" + pt2.x);
}
}
class Point {
int x, y;// 成员变量,属性
【代码4】 Point() {// 无参构造方法
x = 0;
y = 0;
}
public Point(int ix, int 【代码5】) {// 有参构造方法
x = ix;
y = iy;
}
【代码6】 move(int ix, int iy) {// 方法
x += ix;// x=x+ix
y += iy;
}
}
解题
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Point pt = new Point(1, n);// 创建对象
System.out.println("pt.x=" + pt.x);// 取
pt.x = 5;// 修改
System.out.println("pt.x=" + pt.x);
pt.move(3, 3);// 移动
System.out.println("pt.x=" + pt.x);
System.out.println("pt.y=" + pt.y);
Point pt2 = new Point();// 声明对象并new
System.out.println("pt2.x=" + pt2.x);
pt2 = new Point(9, 2);
System.out.println("pt2.x=" + pt2.x);
}
}
class Point {
int x, y;// 成员变量,属性
public Point() {// 无参构造方法
x = 0;
y = 0;
}
public Point(int ix, int iy) {// 有参构造方法
x = ix;
y = iy;
}
public void move(int ix, int iy) {// 方法
x += ix;// x=x+ix
y += iy;
}
}
类描述图 | |
---|---|
类名称 | Point |
成员变量 | int x,y |
成员方法 | void move(int is,int iy) |
构造方法 | Point() Point(int ix,int iy) |
2、类的基本操作简单例子
文件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】/ /命令行窗口输出"我是学生,名字是:奖励"
}
}
解题
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("我是学生,名字是:奖励");//命令行窗口输出"我是学生,名字是:奖励"
}
}
类描述图 | |
---|---|
类名称 | Teacher |
成员方法 | introduceSelf() |
类名称 | Student |
成员方法 | introduceSelf() |
3、关于Point类的操作(2)
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。
文件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;
}
}
解题
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;
}
}
类名称 | Point |
---|---|
成员变量 | Int x,y |
成员方法 | getPoint() setPoint(Point point) |
构造方法 | Point(int x,inty) |
4、关于Point类的操作(3)
下面程序构造一个类来描述屏幕上的一个点,该类的构成包括点的 x 和 y 两个坐标,以及一些对点 进行的操作,包括:取得点的坐标值,对点的坐标进行赋值,编写应用程序生成该类的对象并 对其进行操作。
文件Main.java
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new 【代码1】(10, 10);
origin.getPoint();
【代码2】.setPoint(20, 20);
origin.getPoint();
}
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
【代码3】y = y;
}
public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}
public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}
}
解题
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Point origin = new Point(10, 10);
origin.getPoint();
origin.setPoint(20, 20);
origin.getPoint();
}
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void setPoint(int x1, int y1) {
x = x1;
y = y1;
}
public void getPoint() {
System.out.println("Point x: " + x + ",y: " + y);
}
}
类名称 | Point |
---|---|
成员变量 | private int x; private int y; |
成员方法 | setPoint(int x1, int y1) getPoint() |
构造变量 | Point(int x, int y) |
5、按面向对象要求编程在Employee加入身份证
下面程序在Employee类中加入身份证信息,但类Employee中部分代码缺失.请编写程序代码,使程序运行正确。
文件Main.java
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
String name=reader.nextLine();
String ID=reader.nextLine();
int salary= reader.nextInt();
int year = reader.nextInt();
int month=reader.nextInt();
int day=reader.nextInt();
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println("姓名:"+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
System.out.println("The Main class is end.");
}
}
class Employee {
private String name;//私有域,姓名
private String ID;//私有域,身份证
private double salary;//私有域,工资
private Date hireDay; //私有域,录用日期
//构造方法
public Employee(String n, double s, int year, int month, int day)
{
name = n;//参数,(局部)变量
salary = s;//参数, (局部)变量
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January(1月份是0)
hireDay = calendar.getTime();
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
输入:
姓名
身份证
工资
以空格分隔的日期,形式为YYYY MM DD
输出:
姓名:xxx 身份证:yyyyy 工资:d 这里xxx和yyyyy是字符串,d是double数
The Main class is end.
样例输入:
Romeo
430502199807101121
50000
2014 7 11
样例输出:
姓名:Romeo 身份证:430502199807101121 工资:52500.0
The Main class is end.
解题
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
String name=reader.nextLine();
String ID=reader.nextLine();
int salary= reader.nextInt();
int year = reader.nextInt();
int month=reader.nextInt();
int day=reader.nextInt();
Employee e = new Employee(name,ID,salary,year,month,day);
e.raiseSalary(5);
System.out.println("姓名:"+e. getName()+ " 身份证:" +e.getID()+" 工资:"+ e.getSalary());
System.out.println("The Main class is end.");
}
}
class Employee {
private String name;//私有域,姓名
private String ID;//私有域,身份证
private double salary;//私有域,工资
private Date hireDay; //私有域,录用日期
//构造方法
public Employee(String n,String id, double s, int year, int month, int day)
{
name = n;//参数,(局部)变量
salary = s;//参数, (局部)变量
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
// GregorianCalendar uses 0 for January(1月份是0)
hireDay = calendar.getTime();
}
public String getID() {
// TODO Auto-generated method stub
return ID;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
//"涨工资"计算
public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
}
}
类名称 | |
---|---|
成员变量 | private String name; private String ID;private double salary; private Date hireDay; |
成员方法 | getID() getName() getSalary() getHireDay() raiseSalary(double byPercent) |
构造方法 | public Employee(String n,String id, double s, int year, int month, int day) |
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦