目录
一、图形...1
1、Point.java.1
2、Circle.java.2
2、Square.java.4
3、Triangle.java.5
4、Graphic.java.6
二、复数...8
1、Complex.java.8
2、Prog.java.10
三、时间...11
1、Time.java.11
2、Prog.java.12
四、论坛用户...13
1、User.java.13
2、Prog.java.16
一、图形
1、Point.java
package graphics.point;
public classPoint {
public Point() {
setX(0);
setY(0);
}
public Point(double x,double y) {
super();
this.x = x;
this.y = y;
}
/**
* 得到两点间距离
*
* @param other
* @return
*/
public double dis(Point other) {
return Math.sqrt((this.x - other.x) * (this.x - other.x)
+(this.y - other.y) * (this.y - other.y));
}
public double getX() {
returnx;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
returny;
}
public void setY(double y) {
this.y = y;
}
private double x;
private double y;
}
2、Circle.java
package graphics;
import graphics.point.Point;
public classCircle {
public Circle() {
super();
}
// 只用半径构造
public Circle(double _radius) {
// setCenter(new Point()); // center默认为null?
setRadius(_radius);
}
// 用半径和圆心构造
public Circle(double _radius,double _x, double _y) {
setCenter(new Point(_x, _y));
setRadius(_radius);
}
// 周长
public double perimeter() {
return 2 * Math.PI * getRadius();
}
// 面积
public double area() {
return Math.PI *radius * radius;
}
public Point getCenter() {
returncenter;
}
public void setCenter(Point center){
this.center = center;
}
public double getRadius() {
returnradius;
}
public void setRadius(double radius) {
this.radius = radius;
}
private Pointcenter;
private double radius;
}
2、Square.java
package graphics;
import graphics.point.Point;
public classSquare {
public Square() {
super();
}
/**
* 只用边长构造
*
* @param sideLength
*/
public Square(double sideLength) {
super();
this.sideLength = sideLength;
}
/**
* 用对角的两个点构造
*
* @param x1
* @param y1
* @param x2
* @param y2
*/
public Square(double x1,double y1, double x2,double y2) {
vertex[0] = new Point(x1, y1);
vertex[1] = new Point(x2, y2);
this.sideLength = Math.sqrt(((x1- x2) * (x1 - x2) + (y1 - y2)
*(y1 - y2)) / 2.0);
}
public double perimeter() {
return 4 *this.sideLength;
}
public double area() {
return this.sideLength * this.sideLength;
}
private double sideLength;
private Point[]vertex = new Point[2];
}
3、Triangle.java
package graphics;
import graphics.point.Point;
public classTriangle {
public Triangle() {
super();
}
/**
* 用三条边构造
*
* @param a
* @param b
* @param c
*/
public Triangle(double a,double b, double c) {
if (a + b > c&& a + c > b && b + c > a) {
edge[0] = a;
edge[1] = b;
edge[2] = c;
}else{
System.out.println("不能组成三角形!");
}
}
/**
* 用三个点构造
*
* @param x1
* @param y1
* @param x2
* @param y2
* @param x3
* @param y3
*/
public Triangle(double x1,double y1, double x2,double y2, double x3,
double y3) {
vertex[0] = new Point(x1, y1);
vertex[1] = new Point(x2, y2);
vertex[2] = new Point(x3, y3);
edge[0] = vertex[0].dis(vertex[1]);
edge[1] = vertex[0].dis(vertex[2]);
edge[2] = vertex[1].dis(vertex[2]);
}
/**
* 得到周长
*
* @return
*/
public double perimeter() {
returnedge[0] + edge[1] +edge[2];
}
/**
* 得到面积
*
* @return
*/
public double area() {
double p = (edge[0] +edge[1] + edge[2]) / 2.0;
return Math.sqrt(p * (p-edge[0])* (p - edge[1])* (p -edge[2]));
}
private Point[]vertex = new Point[3];
private double[] edge = newdouble[3];
}
4、Graphic.java
import graphics.Circle;
import graphics.Square;
import graphics.Triangle;
public classGraphic {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CirclemyCircle = newCircle(1);
System.out.println("area: "+ myCircle.area());
System.out.println("perimeter: "+ myCircle.perimeter());
SquaremySquare = newSquare(1);
System.out.println("area: "+ mySquare.area());
System.out.println("perimeter: "+ mySquare.perimeter());
TrianglemyTriangle = newTriangle(3, 4, 5);
System.out.println("area: "+ myTriangle.area());
System.out.println("perimeter: "+ myTriangle.perimeter());
}
}
二、复数
1、Complex.java
package complex;
public classComplex {
public Complex() {
this.setImagNum(0);
this.setRealNum(0);
}
public Complex(double _realNum,double _imagNum) {
this.setImagNum(_imagNum);
this.setRealNum(_realNum);
}
public Complex add(Complexother) {
double i =this.getImagNum() + other.getImagNum();
double r =this.getRealNum() +other.getRealNum();
// Complex c = new Complex(r, i);
// c.imagNum = 0; // 为什么允许直接访问私有成员?在本类中,不管是不是新的引用?
// return c;
return new Complex(r, i);
}
public Complex sub(Complexother) {
double i =this.getImagNum() -other.getImagNum();
double r =this.getRealNum() -other.getRealNum();
return new Complex(r, i);
}
public Complex mul(Complexother) {
double r =this.getRealNum() *other.getRealNum() -this.getImagNum()
*other.getImagNum();
double i =this.getImagNum() *other.getRealNum() +this.getRealNum()
*other.getImagNum();
return new Complex(r, i);
}
public Complex div(Complexother) {
if (Math.abs(other.getImagNum())< Double.MIN_VALUE
&&Math.abs(other.getRealNum()) < Double.MIN_VALUE) {
System.out.println("除数不能为0!");
return null; // 最好不要exit?;C++里怎么返回?
}
double denominator =other.getImagNum() * other.getImagNum()
+other.getRealNum() * other.getRealNum();
double r = (this.getRealNum() *other.getRealNum() +this.getImagNum()
*other.getImagNum())
/denominator;
double i = (this.getImagNum() *other.getRealNum() -this.getRealNum()
*other.getImagNum())
/denominator;
return new Complex(r, i);
}
public double getLength() {
return Math.sqrt(this.getImagNum() *this.getImagNum()
+this.getRealNum()*this.getRealNum());
}
public double getRealNum() {
returnrealNum;
}
public void setRealNum(double realNum) {
this.realNum = realNum;
}
public double getImagNum() {
returnimagNum;
}
public void setImagNum(double imagNum) {
this.imagNum = imagNum;
}
@Override
public String toString() {
return String.format("%.1f+%.1fi",this.realNum,this.imagNum);
}
private double realNum;
private double imagNum;
}
2、Prog.java
import complex.Complex;
public classProg {
public static void main(String[] args) {
Complexa = newComplex(2, 2);
Complexb = newComplex(0, 0);
Complexsum = a.add(b);
Complexdif = a.sub(b);
Complexpro = a.mul(b);
Complexquo = a.div(b);
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("sum=" + sum);
System.out.println("dif=" + dif);
System.out.println("pro=" + pro);
System.out.println("quo=" + quo);
System.out.printf("a.len=%.1f\n",a.getLength());
System.out.printf("b.len=%.1f\n",b.getLength());
}
}
三、时间
1、Time.java
package time;
public classTime {
private int h; // 时
private int m; // 分
private int s; // 秒
public Time() {
h = 0;
m = 0;
s = 0;
}
/**
* 用时、分、秒构造Time对象
*
* @param h
*时
* @param m
*分
* @param s
*秒
*/
public Time(int h,int m, int s) {
super();
this.h = h;
this.m = m;
this.s = s;
}
/**
* 计算时差(不管时间先后)。
*
* @param other
*另一个时间
* @return当前时间与另一个时间的差,不管时间先后。即非负
*/
public Time timeDif(Timeother) {
int thisT =this.h * 3600 +this.m * 60 +this.s;
int otherT = other.h * 3600 + other.m * 60 + other.s;
int difT = thisT - otherT;
if (difT < 0) {
difT= -difT;
}
Timedif = newTime();
dif.h = difT / 3600;
difT%= 3600;
dif.m = difT / 60;
difT%= 60;
dif.s = difT;
return dif;
}
@Override
public String toString() {
return String.format("%d:%d:%d",h,m, s);
}
}
2、Prog.java
import time.Time;
public classProg {
public static void main(String[] args) {
Timea = newTime(1, 21, 2);
Timeb = newTime(22, 1, 0);
Timedif = a.timeDif(b);
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("dif=" + dif);
}
}
四、论坛用户
1、User.java
package user;
public classUser {
@Override
/**
* 输出用户名
*/
public String toString() {
return this.username;
}
public User(String username,String password) {
super();
this.username = username;
this.password = password;
}
public User(String username,String password, String email,
StringrealName, String website, String location, String birthday,
StringaboutMe) {
super();
this.username = username;
this.password = password;
this.email = email;
this.realName = realName;
this.website = website;
this.location = location;
this.birthday = birthday;
this.aboutMe = aboutMe;
}
/**
* 显示个人信息,但不包括密码
*/
public void displayInfo() {
System.out.println("Your information:");
System.out.println("username: "+username);
System.out.println("email: "+email);
System.out.println("realName: "+realName);
System.out.println("website: "+website);
System.out.println("location: "+location);
System.out.println("birthday: "+birthday);
System.out.println("aboutMe: "+aboutMe);
}
public String getUsername() {
returnusername;
}
public void setUsername(Stringusername) {
this.username = username;
}
public String getPassword() {
returnpassword;
}
public void setPassword(Stringpassword) {
this.password = password;
}
public String getEmail() {
returnemail;
}
public void setEmail(String email){
this.email = email;
}
public String getRealName() {
returnrealName;
}
public void setRealName(StringrealName) {
this.realName = realName;
}
public String getWebsite() {
returnwebsite;
}
public void setWebsite(Stringwebsite) {
this.website = website;
}
public String getLocation() {
returnlocation;
}
public void setLocation(Stringlocation) {
this.location = location;
}
public String getBirthday() {
returnbirthday;
}
public void setBirthday(Stringbirthday) {
this.birthday = birthday;
}
public String getAboutMe() {
returnaboutMe;
}
public void setAboutMe(StringaboutMe) {
this.aboutMe = aboutMe;
}
private Stringusername = "";// 防止输出null
private Stringpassword = "";
private Stringemail = "";
private StringrealName = "";
private Stringwebsite = "";
private Stringlocation = "";
private Stringbirthday = "";
private StringaboutMe = "";
}
2、Prog.java
import user.User;
public classProg {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserVictor = newUser("Victor","123");
Victor.displayInfo();
System.out.println();
Victor.setEmail("jiangg2012@hotmail.com");
Victor.displayInfo();
System.out.println();
StringstrBirth = "1991-12-20";
Victor.setBirthday(strBirth);
Victor.displayInfo();
System.out.println();
StringstrAbout = newString("Hello, bool world!");
Victor.setAboutMe(strAbout);
Victor.displayInfo();
System.out.println();
System.out.println(Victor);
}
}