1、定义并测试一个代表员工的Employee类。
员工属性包括“编号”、“姓名”、“基本薪水”、“薪水增长率”,还包括增长额以及计算增长后的工资总额计算方法。
class Employee{
private long num;
private String name;
private double salary;
private double rate;
public Employee(){}
public Employee(long num,String name,double salary,double rate){
this.num=num;
this.name=name;
this.salary=salary;
this.rate=rate;
}
public String getInfo(){
return "雇员编号"+this.num+"、雇员姓名"+this.name+"、雇员工资:"+this.salary+"、雇员工资增长率:"+this.rate;
}
public double getExtra(){
return this.salary*this.rate;
}
public double getsum(){
return this.salary*(1+this.rate);
}
public void setNum(){
this.num=num;
}
public void setName(){
this.name=name;
}
public void setSalary(){
this.salary=salary;
}
public void setRate(){
this.rate=rate;
}
public long getNum(){
return this.num;
}
public String getName(){
return this.name;
}
public double getSalary(){
return this.salary;
}
public double getRate(){
return this.rate;
}
}
class java{
public static void main(String args[]){
Employee emp=new Employee(1000,"张三",1000.0,0.3);
System.out.println(emp.getInfo());
System.out.println(emp.getExtra());
System.out.println(emp.getsum());
}
}
2、设计一个Dog类,由名字、颜色、年龄属性。定义构造方法来初始化类的这些属性,定义方法输出Dog信息。
class Dog(){
private String name;
private String color;
private int age;
public dog(){};
public dog(String name,String color,int age){
this.name=name;
this.color=color;
this.age=age;
}
public String getInfo(){
return "姓名:"+this.name+"、颜色"+this.color+"、年龄"+this.age;
}
public void setName(){
this.name=name;
}
public void setColor(){
this.color=color;
}
public void setAge(){
this.age=age;
}
public String getName(){
return this.name;
}
public String getColor(){
return this.color;
}
public int getAge(){
return this.age;
}
}
class java{
public static void main(String args[]){
Dog dd=new Dog("金毛"、"黄色"、10);
System.out.println(dd.getInfo());
}
}
3、构造一个银行账户类,内容包括:
数据成员的账户名称,用户的账户余额(private类型)
方法包括开户(设置账户名称及余额),利用构造方法完成;查询余额
class bankAccount{
private String accountName;
private double accountMoney;
public bankAccount(){}
public bankAccount(String accountName,double accountMoney){
this.accountName=accountName;
this.accountMoney=accountMoney;
}
public String getInfo(){
return "账户名称:"+this.accountName+"、余额:"+this.accountMoney;
}
public void setName(){
this.accountName=accountName;
}
public void setMoney(){
this.accountMoney=accountMoney;
}
public String getName(){
return this.accountName;
}
public double getMoney(){
return this.accountMoney;
}
}
class javabank{
bankAccount b1=new bankAccount("法外狂徒",1000000.00);
System.out.println(b1.getInfo);
}
4、设计一个表示用户的User类,类中变量由用户名、口令和记录用户个数的变量,定义类的3个构造方法(无参,为用户名赋值,为用户名和口令赋值)、获取和设置口令的方法和返回类信息的方法。
class User{
private String name;
private String psw;
private static int count=0;
public void setName(){
this.name=name;
}
public void setPsw(){
this.psw=psw;
}
public String getName(){
return this.name;
}
public String getPsw(){
return this.psw;
}
public String getInfo(){
return "同户名:"+this.name+"、密码:"+this.psw;
}
public static int getCount(){
return count;
}
public User(){
this("no name","8888888");
}
public User(String name){
this(name,"88888");
}
public User(String name,String psw){
this.name=name;
this.psw=psw;
count++;
}
}
class javaUser{
public static void main(String args[]){
User u1=new User();
User u2=new User("张三");
User u3=new User("法外狂徒","123456");
System.out.println(u1.getInfo());
System.out.println(u2.getInfo());
System.out.println(u3.getInfo());
System.out.println("用户个数:"+User.getCount());
}
}
5、设计一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价。并拥有静态数据成员册数、记录图书的总册数,在构造方法中利用此静态变量为对象编号复制。在主方法中定义多个对象,并求出总册数。
class Book{
private String name;
private long num;
private double price;
private int count=0;
public Book(){}
public String getInfo(){
return "书名:"+this.name+"、编号"+this.num+"、价格:"+this.price;
}
public int getCount(){
return count;
}
public Book(String name,double price){
this.name =name;
this.num=count++;
this.price =price;
}
public void setName(){
this.name =name;
}
public void setNum(){
this.num=num;
}
public void setPrice(){
this.price=price;
}
public String getPrice(){
return price;
}
public String getName(){
return name;
}
public String getNum(){
return num;
}
}
class javaBook{
public static void main(String args[]){
Book b1=new Book("java",78.0);
Book b2=new Book("jsp",88.0);
Book b3=new Book("j2ee",798.0);
System.out.println("一共有"+count+"本书");
}
}