2017.08.14 java开发实战经典 第5章 课后练习题

153319_0hux_3607205.png

153411_PkZd_3607205.png

第一题:

class Address{
	private String country;
	private String province;
	private String city;
	private String street;
	private int postcode;
	public Address(String country,String province,String city,
	String street,int postcode){
		this.country = country;
		this.province = province;
		this.city = city;
		this.street = street;
		this.postcode = postcode;
	}
	public void print(){
		System.out.println("地址: " + this.country +
		this.province + this.city + this.street + "\n邮编" + 
		this.postcode);
	}
}
public class Task16{
	public static void main(String args[]){
		Address ad = new Address("中国","福建省","建瓯市",
		"中山街",353100);
		ad.print();
	}
}

第二题:

class Employee{
	private String number;
	private String name;
	private int wage;
	private int growWage;
	private int time;
	public void setNumber(String number){
		this.number = number;
	}
	public void setName(String name){
		this.name = name;
	}
	public void setWage(int wage){
		this.wage = wage;
	}
	public void setGrowWage(int growWage){
		this.growWage = growWage;
	}
	public void setTime (int time){
		this.time = time;
	}
	public String getNumber(){
		return number;
	}
	public String getName(){
		return name;
	}
	public int getWage(){
		return wage;
	}
	public int getGrowWage(){
		return growWage;
	}
	public int getTime(){
		return time;
	}
	public int grow(){
		return this.getTime()*this.getGrowWage();
	}
	public int sum(){
		return this.wage + this.grow();
	}
	public void print(){
		System.out.println("姓名: " + this.getName() + 
		"\n编号: " + this.getNumber() + "\n基本薪水: " + 
		this.getWage() + "\n薪水增长额: " + this.getGrowWage() + 
		"元/年" + "\n工作年限: " + this.getTime() + "\n薪水增长额: " +
		this.grow() + "\n增长后工资总额: " + this.sum());
	}
}
public class Task17{
	public static void main(String args[]){
		Employee emp = null;
		emp = new Employee();
		emp.setName("张三");
		emp.setNumber("2001");
		emp.setWage(5000);
		emp.setGrowWage(100);
		emp.setTime(5);
		emp.grow();
		emp.sum();
		emp.print();
	}
}

第三题:

public class Task18{
	public static void main(String args[]){
		String st = "want you to know one thing";
		char c[] = st.toCharArray();
		int a = 0;	//定义表示字母 n 出现的次数
		int b = 0;	//定义表示字母 o 出现的次数
		for(int i=0;i<c.length;i++){
			if(c[i]=='n'){
				a++;
			}
			if(c[i]=='o'){
				b++;
			}
		}
		System.out.println(" n 出现的次数为: " + a + 
		"\n o 出现的次数为: " + b );
	}
}

第四题:

class Dog{
	private String name;
	private String colour;
	private int age;
	public void setName(String name){
		this.name = name;
	}
	public void setColour(String colour){
		this.colour = colour;
	}
	public void setAge(int age){
		this.age = age;
	}
	public String getName(){
		return name;
	}
	public String getColour(){
		return colour;
	}
	public int getAge(){
		return age;
	}
	public Dog(String name,String colour,int age){
		this.name = name;
		this.colour = colour;
		this.age = age;
	}
	public void print(){
		System.out.println("狗名: " + this.name + "\n狗色: " +
		this.colour + "\n狗龄: " + this.age );
	}
}
public class Task19{
	public static void main(String args[]){
		Dog dog = new Dog("张三","红色",2);
		dog.print();
	}
}

第五题:

class User{
	private String name;
	private String password;
	private int number;
	public User(){
		
	}
	public User(String name){
		this.name = name;
	}
	public User(String name,String password){
		this.name = name;
		this.password = password;
	}
	public void setPassword(String password){
		this.password = password;
	}
	public String getPassword(){
		return password;
	}
	public User fun(){
		User user = new User(this.name);
		user.setPassword(this.password);
		return user;
	}
}

第六题:

public class Task23{
	public static void main(String args[]){
		String a = "Java 技术学习班 20070326";
		System.out.println("(1)" + a.substring(11));
		String t = "MLDN JAVA";
		System.out.println("(2)" + t.replace("JAVA","J2EE") );
		System.out.println("(3)" + a.charAt(8-1));
		System.out.println("(4)" + a.replace("0",""));
		System.out.println("(5)" + a.replace(" ",""));
		String sfz = "350783191010238754";
		System.out.println("(6)" + sfz.substring(6,14));		
	}
}

第七题:

class Personnel{
	private String number;
	private String name;
	private int wage;
	private String department;
	public Personnel(String number){
		this.number = number;
		this.name = "无名氏";
		this.wage = 0;
		this.department = "未定";
	}
	public Personnel(String number,String name){
		this.number = number;
		this.name = name;
		this.wage = 1000;
		this.department = "后勤";
	}
	public Personnel(String number,String name,String department,int wage){
		this.name = name;
		this.number = number;
		this.wage = wage;
		this.department = department;
	}
	public Personnel(){

	}
	public void print(){
		System.out.println("工号: " + this.number + "\n姓名: " +
		this.name + "\n部门: " + this.department + "\n薪水: " + this.wage);
	}
}
public class Task21{
	public static void main(String args[]){
		Personnel per1 = new Personnel();
		Personnel per2 = new Personnel("20051420");
		Personnel per3 = new Personnel("10056352","张老三");
		Personnel per4 = new Personnel("30054520","李二狗","教育部",800);
		per1.print();
		per2.print();
		per3.print();
		per4.print();
	}
}

第八题:

class BankAccount{
	private String name;
	private double balance;
	public BankAccount(String name,double balance){
		this.name = name;
		this.balance = balance;
	}
	public double lookup(){
		return this.balance;
	}
}
public class Task22{
	public static void main(String args[]){
		BankAccount ac = new BankAccount("李二",150023601.2);
		System.out.println(ac.lookup());
	}
}

第九题:

class Book{
	private String name;
	private static String number;
	private String price;
	private static int tome;
	public static String getNumber(){
		return number;
	}
	public static int getTome(){
		return tome;
	}
	public Book(){
		tome++;
		this.number = "1000" + tome;
	}
}
public class Task24{
	public static void main(String args[]){
		Book b[] = new Book[10];
		for(int i=0;i<b.length;i++){
			b[i] = new Book();
			System.out.println("第" + (i+1) + "本书,编号为: " + Book.getNumber());
		}
		System.out.println("总册数为: " + Book.getTome());
			
	}
}

 

转载于:https://my.oschina.net/u/3607205/blog/1509902

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值