《第一行代码java》课后编程题(第三章)

第三章面向对象基本概念

1.编写并测试一个代表地址的Address类,地址信息由:国家,省份,城市,街道,邮编组成,并可以返回完整的地址信息。
代码如下

package www.zbq.com;

class Address {
	private String Country;
	private String Province;
	private String City;
	private String Street;
	private String Postcode;

	public Address(String Country, String Province, String City, String Street, String Postcode) {
		this.Country = Country;
		this.Province = Province;
		this.City = City;
		this.Street = Street;
		this.Postcode = Postcode;
	}

	public String Information() {
		return "国家" + this.Country + "省份" + this.Province + "城市" + this.City + "街道" + this.Street + "邮编"
				+ this.Postcode;
	}
}

public class Test {
	public static void main(String[] args) {
		Address addrsss = new Address("ZhongGuo", "XinJiang", "WuluMuqi", "TianShan", "00000");
		System.out.println(addrsss.Information());
	}
}

2.定义并测试一个代表员工的Employee类。员工属性包括“编号”,“姓名”,“基本薪水”,“薪水增长额”;还包括“计算增长后的工资总额”的操作方法。
代码如下

package www.zbq.com;

class Employee {
	private String Number;
	private String Name;
	private double Salary;
	private double Add;

	public Employee(String Number, String Name, double Salary, double Add) {
		this.Number = Number;
		this.Name = Name;
		this.Salary = Salary;
		this.Add = Add;
	}

	public double sum(double Salary, double Add) {
		return Salary + Add;
	}

	public void print() {
		System.out.println("编号;" + this.Number + "姓名:" + this.Name + "基本工资:" + this.Salary + "增加薪资:" + this.Add
				+ "增加之后的工资:" + this.sum(Salary, Add));
	}
}

public class Test {
	public static void main(String[] args) {
		Employee em = new Employee("12348", "xiaowang", 2300, 3000);
		em.print();
	}
}

3.编写程序将字符串“want you to konw one thing”,统计出字母“n”和字母“o”的出现次数
代码如下

package www.zbq.com;

public class Test {
	public static void main(String[] args) {
		String str = "want you to konw one thing";
		char data[] = str.toCharArray();
		int a = 0;
		int b = 0;
		for (int x = 0; x < data.length; x++) {
			if (data[x] == 'n') {
				a++;
				continue;
			}
			if (data[x] == 'o') {
				b++;
			}
		}
		System.out.println("n出现的次数是" + a + "o出现的次数是" + b);
	}
}

4.设计一个Dog类,有名字,颜色,年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息。编写应用程序使用Dog类。
代码如下

package www.zbq.com;

class Dog {
	private String Name;
	private String Color;
	private int Age;

	public Dog(String Name, String Color, int Age) {
		this.Name = Name;
		this.Color = Color;
		this.Age = Age;
	}

	public void Information() {
		System.out.println("名字" + this.Name + "颜色" + this.Color + "年龄" + this.Age);
	}
}

public class Test {
	public static void main(String[] args) {
		Dog dog = new Dog("Jack", "black", 12);
		dog.Information();
	}
}

5.字符串操作
(1)从字符串“MLDN中心Java技术学习班20130214”中提取开班日期。
代码如下

package www.zbq.com;

public class Test {
	public static void main(String[] args) {
		String str = "MLDN中心Java技术学习班20130214";
		String str1 = str.substring(15);
		System.out.println(str1);
	}
}

(2)将“MLDNJAVA高端技术培训”字符串中的“JAVA”替换为“JAVAEE”
代码如下

package www.zbq.com;

public class Test {
	public static void main(String[] args) {
		String str = "MLDNJAVA高端技术培训";
		String str1 = str.replaceAll("JAVA", "JAVAEE");
		System.out.println(str1);
	}
}

(3)取出“Java技术学习班20130214”中的第八个字符
代码如下

package www.zbq.com;

public class Test {
	public static void main(String[] args) {
		String str = "Java技术学习班20130214";
		String str1 = str.substring(8, 9);
		System.out.println(str1);
	}
}

(4)清除“Java技术学习班20130214”中的所有的“0”
代码如下

package www.zbq.com;

public class Test {
	public static void main(String[] args) {
		String str = "Java技术学习班20130214";
		String str1 = str.replaceAll("0", "");
		System.out.println(str1);
	}
}

(5)从任意给定的身份证号码中提取此人的出生日期
代码如下

package www.zbq.com;

public class Test {
	public static void main(String[] args) {
		String str = "正常的身份证号码";
		System.out.println(Extract(str));
	}

	public static String Extract(String str) {
		return str.substring(6, 14);
	}
}

6.编写一个银行账户类,类的构成包括
数据成员:用户的账户名称,用户的账户余额
方法包括:开户(设置账户名称及余额),利用构造方法完成
查询余额
代码如下

package www.zbq.com;

class BankAccounts {
	private String User;
	private String AccountsName;
	private Money money;

	public BankAccounts(String User, String AccountsName) {
		this.User = User;
		this.AccountsName = AccountsName;
	}

	public String getUser() {
		return User;
	}

	public void setUser(String user) {
		User = user;
	}

	public String getAccountsName() {
		return AccountsName;
	}

	public void setAccountsName(String accountsName) {
		AccountsName = accountsName;
	}

	public Money getMoney() {
		return money;
	}

	public void setMoney(Money money) {
		this.money = money;
	}

	public void print() {
		System.out.println("用户:" + this.User + "账户:" + this.AccountsName);
	}
}

class Money {
	private double Balance;

	public Money(double Balance) {
		this.Balance = Balance;
	}

	public double getBalance() {
		return Balance;
	}

	public void setBalance(double balance) {
		Balance = balance;
	}

	public void prints() {
		System.out.println("余额:" + this.Balance);
	}
}

public class Test {
	public static void main(String[] args) {
		BankAccounts a = new BankAccounts("张", "1541515");
		a.setMoney(new Money(12990));
		a.print();
		a.getMoney().prints();
	}

}

因为本人的这个理解能力有限,所以对这道题的题目意思可能没有明白,我的这道题的主要思路来源就是,本书的第三章中的数据表与简单java类映射的内容,如有不足,敬请指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值