1.了解对象与对象引用的关系。
2.掌握类的定义,熟悉属性、构造函数、方法的作用。
3.理解static的作用和使用方法。
1.设计一个顾客类Customer。
2.设计一个名为Rectangle的类
3.定义一个点(Point) 类用来表示三维空间中的点。
4.编程计算一个点 ( Point 对象) 是否在圆 ( Cricle 对象)内。
5.定义一个表示学生的类Student。
1.顾客管理设计
1)定义一个顾客类Customer,要求有属性:
id:long型,代表编号
name:String类对象,代表姓名
age:int型,代表年龄
marry:boolen型,代表婚否(其中:true表示已婚,false表示未婚)
phone:String类对象,代表联系电话
2)方法有:
(1)Customer(long i,String n,int a,boolean m,String p):有参构造函数,分别初始化编号、姓名、年龄、性别和联系电话。
(2)public String toString():以“姓名:年龄:联系电话”的形式作为方法的返回值。
(3)getXX()方法:得到属性值的方法,其中XX表示类中的各属性名。
(4)setXX(dd)方法:各属性值设置方法,其中XX表示类中的各属性名。
(5)编写main函数对类进行测试。
源代码:
public class Customer {
long id;
String name;
int age;
boolean marry;
String phone;
Customer(long i,String n,int a,boolean m,String p){
id = i;
name = n;
age = a;
marry = m;
phone = p;
}
public void set_id(long i){
id = i;
}
public void set_name(String n){
name = n;
}
public void set_age(int a){
age = a;
}
public void set_marry(boolean m){
marry = m;
}
public void set_phone(String p){
phone = p;
}
public long get_id() {
return id;
}
public String get_name() {
return name;
}
public int get_age() {
return age;
}
public boolean get_marry() {
return marry;
}
public String get_phone() {
return phone;
}
//以“姓名:年龄:联系电话”的形式作为方法的返回值。
public String toString(){
return "[编号:" + id + ", 姓名:" + name + ", 年龄:" + age + ", +是否结婚:"+marry+", +联系电话:" + phone +" ]";
}
public static void main(String[] args) {
Customer c = new Customer(1,"张三",20,true,"110");
System.out.println(c.toString());
c.age = 19;
c.id = 2;
c.name = "李四";
c.phone = "120";
c.marry = false;
System.out.println(c.toString());
}
}
2.写一个名为Rectangle的类表示同一种颜色的矩形类。其成员属性包括宽width、高height,类属性包括颜色color(默认颜色是蓝色),width和height都是double型的,而color则是String类型的。要求该类具有:
1)使用构造函数完成各属性的初始赋值。
2)使用getter和setter的形式完成属性的访问及修改。
3)提供计算面积的getArea()方法。
4)合理的toString方法。
5)编写main函数对类进行测试。
源代码:
public class Rectangle {
double width;
double height;
static String color = "蓝色";
//使用构造函数完成个属性的初始赋值
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
//使用getter和setter的形式完成属性的访问及修改
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public static String getColor() {
return color;
}
public static void setColor(String color) {
Rectangle.color = color;
}
//提供计算面积的getArea()方法
public double getArea(){
return width*height;
}
//合理的toString方法
public String toString(){
return "高:"+height+"\t宽"+width+"\t颜色:"+color+"\t面积:"+getArea();
}
public static void main(String[] args) {
Rectangle r = new Rectangle(10,20);
System.out.println(r.toString());
r.width = 12;
r.height = 8;
r.setColor("红色");
System.out.println(r.toString());
}
}
3.定义一个点(Point) 类用来表示三维空间中的点 (有三个坐标),要求如下:
1)可以生成具有特定坐标的点对象。
2)提供可以设置三个坐标的方法。
3)提供可以计算该点距另外点距离平方的方法。
4)编写主类程序验证。
源代码:
public class Point {
int x,y,z;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
public Point(int x, int y, int z) {
super();
this.x = x;
this.y = y;
this.z = z;
}
public double getDistance(Point p)
{
return (x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z);
}
public static void main(String[] args) {
Point p1=new Point(0,0,0);
Point p2=new Point(1,2,4);
System.out.println("距原点的距离平方是:"+p2.getDistance(p1));
System.out.println("距原点的距离平方是:"+p2.getDistance(new Point(2,4,4)));
p2.setX(2);
p2.setY(0);
p2.setZ(0);
System.out.println("修改坐标后,距任一点的距离平方是:" + p2.getDistance(p1));
}
}
4. 编程计算一个点 ( Point 对象) 是否在圆 ( Cricle 对象)内。
1)设计一个类代表二维空间的一个点。
2)设计一个类Circle代表二维空间的一个圆,要求两个成员变量:一个是圆心,一个是半径。提供计算面积的方法。
3)为上述 Circle 类添加一个方法,计算一个点 ( Point 对象) 是否在圆 ( Cricle 对象)内,并写程序验证。
源代码:
import java.util.Scanner;
public class Circle {
double x,y,z;
//z为圆的半径,(x,y)为一点的坐标
public Circle(double x,double y,double z){
this.x = x;
this.y = y;
this.z = z;
}
public double get_area(){
return Math.PI*z*z;
}
public boolean isIn (double m , double n) {//判断点是否在圆内,传入随便一点的坐标
if( Math.pow(x-m,2) + Math.pow(y-n,2) >= Math.pow(z, 2)) {
final boolean b = false;
return b;
} else {
return true ;
}
}
public static void main(String[] args) {
Circle c = new Circle(1,2,3);
System.out.println("圆的面积为:"+c.get_area());
System.out.println("请输入x坐标:");
Scanner sc = new Scanner(System.in);
double m = sc.nextDouble();
System.out.println("请输入y坐标:");
double n = sc.nextDouble();
if(c.isIn(m,n)){
System.out.println("该点在园内");
}else{
System.out.println("该点不在园内");
}
}
}
5. 编写一个Java程序,定义一个表示学生的类Student,该类包括:
1)这个类的属性有:“学号”、“班号”、“姓名”、“性别”、“年龄”,每个属性的类型请参考构造方法。
2)为每个属性编写getter和setter方法。
3)编写构造方法为属性赋值。
public Student(long studentID,int classID,String name,String sex,int age)
4)为类Student增加一个方法public String toString(),该方法把Student类的对象的所有属性信息组合成一个字符串以便输出显示。
5)编写一个Java Application程序,创建Student类的对象,并验证新增加的功能。
源代码:
import java.awt.dnd.DragGestureEvent;
public class Student {
long studentID;
int classID;
String name;
String sex;
int age;
public long getStudentID() {
return studentID;
}
public void setStudentID(long studentID) {
this.studentID = studentID;
}
public int getClassID() {
return classID;
}
public void setClassID(int classID) {
this.classID = classID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(long studentID, int classID, String name, String sex, int age){
this.studentID = studentID;
this.classID = classID;
this.name = name;
this.sex = sex;
this.age = age;
}
public String toString(){
return "学号:"+studentID+"班号:"+classID+"姓名:"+name+"性别:"+sex+"年龄:"+age;
}
public static void main(String[] args) {
Student s1 = new Student(22,2,"张三","男",20);
Student s2 = new Student(23,3,"李四","男",21);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
通过本章学习:
了解对象与对象引用的关系,掌握类的定义,熟悉属性、构造函数、方法的作用,理解static的作用和使用方法。