函数2

普通账户和支票账户
class Account{
int id;
int balance;
Account() {
id=0;
balance=0;
}
Account(int id,int balance){
this.balance=balance;
this.id=id;
}
void setBalance(int balance) {
this.balance=balance;
}
int getBalance() {
return balance;
}
boolean withdraw(int money) {
if(money>balance) {
return false;
}else {
balance-=money;
return true;
}
}
void deposit(int money) {
balance+=money;
}
public String toString() {
return "The balance of account “+id+” is "+balance;
}
}

class CheckingAccount extends Account{

int overdraft;
CheckingAccount(){
	super(0,0);
	overdraft=0;
}
CheckingAccount(int id,int balance,int overdraft){
	super(id,balance);
	this.overdraft=overdraft;
}
boolean withdraw(int money) {
	if(balance-money>overdraft||balance-money<-overdraft) {
		return false;
	}else {
		balance-=money;
		return true;
	}	
}

}
人口统计
public static int numofHan(String data[]){
String s = “汉族”;
int num = 0;
for(String s_t: data){
if( s_t.indexOf(s) >= 0 ){
num ++;
}
}
return num;
}
教师、学生排序
class Student extends Person{
private int sno;
private String major;

public Student(int sno, String name, String gender, int age, String major) {
    super(name, gender, age);
    this.sno = sno;
    this.major = major;
}

@Override
public int compareTo(Object o) {
    return -(this.sno - ((Student)o).getSno());
}

public int getSno() {
    return sno;
}

public String getMajor() {
    return major;
}

public void setSno(int sno) {
    this.sno = sno;
}

public void setMajor(String major) {
    this.major = major;
}

}

class Teacher extends Person{
private int tno;
private String subject;

public Teacher(int tno, String name, String gender, int age, String subject) {
    super(name, gender, age);
    this.tno = tno;
    this.subject = subject;
}

@Override
public int compareTo(Object o) {
    return this.getAge() - ((Teacher)o).getAge();
}

public int getTno() {
    return tno;
}

public String getSubject() {
    return subject;
}

public void setTno(int tno) {
    this.tno = tno;
}

public void setSubject(String subject) {
    this.subject = subject;
}

}

class MyTool{
public static void separateStu_T(List persons,List teachers,List students){
for( Object o : persons ){
if( o instanceof Student ){
students.add(o);
}
else if( o instanceof Teacher ){
teachers.add(o);
}
}
}
}
矩形
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x=x;
this.y=y;
}
@Override
public String toString() {
return"("+x+","+y+")";
}
public void move(int dx, int dy) {
x=x+dx;
y=y+dy;
}
public double distance(Point p) {
int xx=(this.x-p.x)(this.x-p.x);
int yy=(this.y-p.y)
(this.y-p.y);
double sum=xx+yy;
return Math.sqrt(sum);
}
}
class Dimension {
private int width;
private int height;
public Dimension(int width, int height) {
this.height=height;
this.width=width;
}
@Override
public String toString() {
return width+" by “+height;
}
public void resize(double widthScale, double heightScale) {
width*=widthScale;
height*=heightScale;
}
public int area() {
return width*height;
}
}
class Rectangle {
private Point topleft;
private Dimension size;
public Rectangle(Point topleft, Dimension size) {
this.topleft=topleft;
this.size=size;
}
public String toString() {
return"Rectangle at “+topleft.toString()+”:”+size.toString();
}
public void move(int dx, int dy) {
topleft.move(dx, dy);
}
public void resize(double widthScale, double heightScale) {
size.resize(widthScale, heightScale);
}
public double area() {
return size.area();
}
public double distance(Rectangle r) {
return this.topleft.distance(r.topleft);
}
}
** TDVector**
TDVector(){
this.x=0;
this.y=0;
}
TDVector(double x,double y){
this.x=x;
this.y=y;
}
TDVector(TDVector t){
this.x=t.x;
this.y=t.y;
}
void setX(double x) {
this.x=x;
}
void setY(double y) {
this.y=y;
}
double getX() {
return x;
}
double getY() {
return y;
}
TDVector add(TDVector t) {
TDVector tt=new TDVector();
tt.x=this.x+t.x;
tt.y=this.y+t.y;
return tt;
}
顺序表-插入元素函数练习
public void insert(int pos,E e) {
this.element[length++]=1;
this.element[pos]=e;
}
设计Worker类及其子类

class Worker {
private String Name;
private double Rate;
public Worker(String _Name,double _Rate){
Name = _Name;
Rate = _Rate;
}
void setName(String _name) {
Name = _name;
}
double getRate(){
return Rate;
}
void setRate(double _Rate){
Rate = _Rate;
}
void pay(int time){
System.out.println(“Not Implemented”);
}
}

class HourlyWorker extends Worker {
public HourlyWorker(String string, double d) {
super(string ,d);
}

void pay(int time){
	if(time > 40)
		System.out.println(this.getRate()*time*2);
	else
		System.out.println(this.getRate()*time);
}

}
class SalariedWorker extends Worker {
public SalariedWorker(String string, double d) {
super(string ,d);
}
void pay(){
System.out.println(this.getRate()*40);
}
void pay(int time){
System.out.println(this.getRate()*40);
}
}
二维向量定义及相加
TDVector(){
this.x=0;
this.y=0;
}
TDVector(double x,double y){
this.x=x;
this.y=y;
}
TDVector(TDVector t){
this.x=t.x;
this.y=t.y;
}
void setX(double x) {
this.x=x;
}
void setY(double y) {
this.y=y;
}
double getX() {
return x;
}
double getY() {
return y;
}
TDVector add(TDVector t) {
TDVector tt=new TDVector();
tt.x=this.x+t.x;
tt.y=this.y+t.y;
return tt;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值