1.定义一个表示学生的student类,包括的域有学号、姓名、性别、年龄,包括的方法有获得学号、姓名、性别、年龄及修改年龄。编写Java程序创建student类的对象及测试其方法的功能.
2.为第1题的student类定义构造函数初始化所有的域,增加一个方法public String printInfo()把student类对象的所有域信息组合形成一个字符串,并在主类中创建学生对象及测试各方法的功能.
3.设计一个Point类,该类包含两个int型成员变量:x、y,一个color型成员变量mycolor,请给出此类的构造方法,分别是一个不带参数的,一个带两个参数的,一个带三个参数的构造方法。用下面的main方法测试:
public static void main(String[] args)
{
Point A=new Point();
Point B=new Point(50,60);
Point C=new Point(100,200,Color.red);
System.out.println(“B:(”+B.getX()+”,”+B.getY()+”)”+”color: “+B.getColor());
A.setX(100);
A.setY(200);
A.setColor(Color.red);
System.out.println(“A==B?”+A.equals(B));
}
程序代码:
(1,2)
public class StudentTest {
public static void main(String[] args) {
Student stu=new Student(1001,"李明","男",23);
System.out.println(stu.printInfo());
stu.alerAge(25);
System.out.println(stu.printInfo());
}
}
class Student
{
private int id;
private String name;
private String sex;
private int age;
public Student(int id,String name,String sex,int age)
{
this.id=id;
this.setName(name);
this.setSex(sex);
this.setAge(age);
}
public void setName(String name)
{
if(name==null||name=="")
this.name="姓名未知";
else
this.name=name;
}
public void setSex(String sex)
{
if(sex=="男"||sex=="女")
this.sex=sex;
else
this.sex="输入错误 !";
}
public void setAge(int age)
{
if(age>0&&age<100)
this.age=age;
else
this.age=0;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public String getSex()
{
return sex;
}
public int alerAge(int age1)
{
this.age=age1;
return this.age;
}
public String printInfo()
{
return "我的信息:"+"\n"+"学号:"+id+" 姓名:"+name+" 性别:"+sex+" 年龄"+age;
}
}
3.import java.awt.Color;
public class Point {
int x;
int y;
Color mycolor;
public Point()
{
this(0,0,Color.black);
}
public Point(int x,int y)
{
this.setX(x);
this.setY(y);
}
public Point(int x,int y,Color mycolor)
{
this.setX(x);
this.setY(y);
this.setColor(mycolor);
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setColor(Color mycolor)
{
this.mycolor=mycolor;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public Color getColor()
{
return mycolor;
}
public static void main(String[] args) {
Point A=new Point();
Point B=new Point(50,60);
// Point C=new Point(100,200,Color.red);
System.out.println("B:("+B.getX()+","+B.getY()+")"+"color: "+B.getColor());
A.setX(100);
A.setY(200);
A.setColor(Color.red);
System.out.println("A==B?"+A.equals(B));
}
}
2.为第1题的student类定义构造函数初始化所有的域,增加一个方法public String printInfo()把student类对象的所有域信息组合形成一个字符串,并在主类中创建学生对象及测试各方法的功能.
3.设计一个Point类,该类包含两个int型成员变量:x、y,一个color型成员变量mycolor,请给出此类的构造方法,分别是一个不带参数的,一个带两个参数的,一个带三个参数的构造方法。用下面的main方法测试:
public static void main(String[] args)
{
Point A=new Point();
Point B=new Point(50,60);
Point C=new Point(100,200,Color.red);
System.out.println(“B:(”+B.getX()+”,”+B.getY()+”)”+”color: “+B.getColor());
A.setX(100);
A.setY(200);
A.setColor(Color.red);
System.out.println(“A==B?”+A.equals(B));
}
程序代码:
(1,2)
public class StudentTest {
public static void main(String[] args) {
Student stu=new Student(1001,"李明","男",23);
System.out.println(stu.printInfo());
stu.alerAge(25);
System.out.println(stu.printInfo());
}
}
class Student
{
private int id;
private String name;
private String sex;
private int age;
public Student(int id,String name,String sex,int age)
{
this.id=id;
this.setName(name);
this.setSex(sex);
this.setAge(age);
}
public void setName(String name)
{
if(name==null||name=="")
this.name="姓名未知";
else
this.name=name;
}
public void setSex(String sex)
{
if(sex=="男"||sex=="女")
this.sex=sex;
else
this.sex="输入错误 !";
}
public void setAge(int age)
{
if(age>0&&age<100)
this.age=age;
else
this.age=0;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public String getSex()
{
return sex;
}
public int alerAge(int age1)
{
this.age=age1;
return this.age;
}
public String printInfo()
{
return "我的信息:"+"\n"+"学号:"+id+" 姓名:"+name+" 性别:"+sex+" 年龄"+age;
}
}
3.import java.awt.Color;
public class Point {
int x;
int y;
Color mycolor;
public Point()
{
this(0,0,Color.black);
}
public Point(int x,int y)
{
this.setX(x);
this.setY(y);
}
public Point(int x,int y,Color mycolor)
{
this.setX(x);
this.setY(y);
this.setColor(mycolor);
}
public void setX(int x)
{
this.x=x;
}
public void setY(int y)
{
this.y=y;
}
public void setColor(Color mycolor)
{
this.mycolor=mycolor;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public Color getColor()
{
return mycolor;
}
public static void main(String[] args) {
Point A=new Point();
Point B=new Point(50,60);
// Point C=new Point(100,200,Color.red);
System.out.println("B:("+B.getX()+","+B.getY()+")"+"color: "+B.getColor());
A.setX(100);
A.setY(200);
A.setColor(Color.red);
System.out.println("A==B?"+A.equals(B));
}
}