面向对象(基础篇习题自己打的答案,错了请大胆的批评和教我怎么改(哈哈))
来源:互联网 发布:linux piwik 编辑:程序博客网 时间:2024/05/01 10:15
习题1、编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息;
public class Demo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Address a = new Address("China", "guangdong", "guangzhou", "tianhe",
518099);
System.out.println("国家" + a.getCountry() + "、省份" + a.getProvince()
+ "、城市" + a.getCity() + "、街道" + a.getStreet() + "、邮编"
+ a.getPost());
}
}
class Address {
String country;// 国家
String province;// 省份
String city;// 城市
String street;// 街道
int post;// 邮编
// 构造方法初始化属性
public Address(String country, String province, String city, String street,
int post) {
this.setCountry(country);
this.setProvince(province);
this.setCity(city);
this.setStreet(street);
this.setPost(post);
}
// set方法
public void setCountry(String country) {
this.country = country;
}
public void setProvince(String province) {
this.province = province;
}
public void setCity(String city) {
this.city = city;
}
public void setStreet(String street) {
this.street = street;
}
public void setPost(int post) {
this.post = post;
}
// get方法
public String getCountry() {
return this.country;
}
public String getProvince() {
return this.province;
}
public String getCity() {
return this.city;
}
public String getStreet() {
return this.street;
}
public int getPost() {
return this.post;
}
}
习题2、定义并测试一个代表员工的Employee类。员工属性包括"编号"、“姓名”、“基本薪水”、“薪水增长额”,还包括计算薪水增加额及计算增长后的工资总额的操作方法;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee me = new Employee("abc123", "eason", 4500.0, 1.2);
System.out.println("编号:" + me.getNumber() + " 姓名" + me.getName()
+ " 基本工资" + me.getBasal() + " 增长率" + me.getIncrease() + " 总工资"
+ me.salary());
}
}
class Employee {
private String number;// 编号 private 表示封装,外部无法访问此属性,必须通过方法的调用
private String name;// 姓名
private double basal;// 基本薪水
private double increase;// 增长额
// double salary;//总工资
// 构造方法设定初始值
public Employee(String number, String name, double basal, double increase) {
this.setNumber(number);
this.setName(name);
this.setBasal(basal);
this.setIncrease(increase);
}
// set方法
public void setNumber(String number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setBasal(double basal) {
this.basal = basal;
}
public void setIncrease(double increase) {
this.increase = increase;
}
// get方法
public String getNumber() {
return this.number;
}
public String getName() {
return this.name;
}
public double getBasal() {
return this.basal;
}
public double getIncrease() {
return this.increase;
}
public double salary() {
return basal * increase;
}
}
习题3、编写程序,统计出字符串“want you to know one thing”中字母n和字母o的出现次数;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 0;
int o = 0;
String str = "want you to know one thing";
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++) {
if (a[i] == 'n') {
n++;
}
if (a[i] == 'o') {
o++;
}
}
System.out.println("n:" + n + "、o:" + o);
}
}
习题4、设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息,编写应用程序使用Dog类;
public class Demo3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 0;
int o = 0;
String str = "want you to know one thing";
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++) {
if (a[i] == 'n') {
n++;
}
if (a[i] == 'o') {
o++;
}
}
System.out.println("n:" + n + "、o:" + o);
}
}
习题5、设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,定义类的3个构造方法(无参、为用户赋值、为用户名和口令赋值),获取和设置口令的方法和返回类信息的方法。
package Mycode;
public class Demo5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
User n1 = new User();
User1 n2 = new User1("小路", "adc");
User1 n3 = new User1("小法");
User1 n4 = new User1("小智", "cba");
User1 n5 = new User1("小球", "hml");
User1 n6 = new User1("小区");
n3.setPassword("12345");
System.out.println("姓名" + n2.getName() + "、密码" + n2.getPassword());
System.out.println("姓名" + n3.getName() + "、密碼" + n3.getPassword());
System.out.println("姓名" + n4.getName() + "、密码" + n4.getPassword());
System.out.println("姓名" + n5.getName() + "、密码" + n5.getPassword());
System.out.println("姓名" + n6.getName());
System.out.println("人数" + n6.getCount());
}
}
class User1 {
private String name;// 用户名
private String password;// 密码
private static int count = 0;// 统计用户人数 用static 表示能被其他对象所共享使用
// 构造方法 无参
public User1() {
}
public User1(String name) {
count++;
this.setName(name);
}
public User1(String name, String password) {
count++;
this.setName(name);
this.setPassword(password);
}
// set方法
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
// get方法
public String getName() {
return this.name;
}
public String getPassword() {
return this.password;
}
// 返回用户个数
public int getCount() {
return count;
}
}
习题6、字符串操作:
1.从字符串“Java技术学习班20070326”中提取开班日期
2、将“LLLL Java”字符串中的“java”替换为J2EE
3、取出“Java技术学习班20070326”中的第8个字符。
4、清除“Java技术学习班20070326”中所有的0.
5、清除“Java技术学习班 20070326 kkkk 老师”中所有的空格。
6、从任意给定的身份证号码中提取此人的出生日期。
public class Demo61 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "Java技术学习班20070326";
System.out.println(str1.substring(9, 17));
String str2 = "LLLL Java";
System.out.println(str2.replaceAll("Java", "J2EE"));// 如果寻找的Java不在str2里面,则不替换
String str3 = "Java技术学习班20070326";
System.out.println(str3.charAt(8));
String str4 = "Java技术学习班20070326";
System.out.println(str4.replace("0", ""));
String str5 = "Java技术学习班 20070326 kkkk 老师";
System.out.println(str5.replace(" ", ""));
String str6 = "362502197812208233";
System.out.println(str6.substring(6, 17));
}
}
7、编写一个公司员工类。
(1)数据成员:员工号、姓名、薪水、部门。
(2)方法:
① 利用构造方法完成设置信息。
一、单参,只传递员工号,则员工姓名:无名氏,薪水:0,部门:未定。
二、双参,传递员工号,姓名,则员工薪水为1000,部门:后勤。
三、四参,传递员工号、姓名、部门、薪水。
四、无参,则均为空值。
②显示信息。
public class Demo7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//调用
ComEm n1 = new ComEm();
ComEm n2 = new ComEm("201130111298");
ComEm n3 = new ComEm("201130111298","Nick");
ComEm n4 = new ComEm("201130111298","Nick",8921.8,"CEO");
n1.printf();
n2.printf();
n3.printf();
n4.printf();
}
}
class ComEm {
private String number;// 员工号
private String name;// 姓名
private double salary;// 薪水
private String department;// 部门
// 构造方法,多态,
public ComEm() {
//this.salary=null;
}
public ComEm(String number) {
this.setNumber(number);
this.setName("无名氏");
this.setSalary(0);
this.setDepartment("未定");
}
public ComEm(String number, String name) {
this.setName(name);
this.setNumber(number);
this.setSalary(1000);
this.setDepartment("后勤");
}
public ComEm(String number, String name, double salary, String department) {
this.setNumber(number);
this.setDepartment(department);
this.setName(name);
this.setSalary(salary);
}
// set方法
public void setNumber(String number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setDepartment(String department) {
this.department = department;
}
// get方法
public String getNumber() {
return this.number;
}
public String getName() {
return this.name;
}
public double getSalary() {
return this.salary;
}
public String getDepartment() {
return this.department;
}
public void printf() {
System.out.println("员工号:" + this.number + " 姓名:" + this.name + " 薪水:"
+ this.salary + " 部门:" + this.department);
}
}
习题8、构造一个银行账户类,类的构造包括如下内容:
(1)数据成员用户的账户名称、用户的账户余额(private数据类型)。
(2)方法包括开户(设置账户名称及余额),利用构造方法完成。
(3)查询余额。
public class Demo8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Bank a = new Bank("a", 888.82);
System.out.println("用户名" + a.name + " 余额" + a.getBalance());
}
}
class Bank {
String name;// 账户
private double balance;// 余额
// 构造方法
public Bank(String name, double balance) {
this.setName(name);
this.setBalance(balance);
}
// set方法
public void setName(String name) {
this.name = name;
}
public void setBalance(double balance) {
this.balance = balance;
}
// get方法
public String getName() {
return this.name;
}
public double getBalance() {
return this.balance;
}
}
习题9、声明一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数、记录图书的总册数,在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义对象数组,并求出总册数;
package Mycode;
public class Demo9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Book book[] = { new Book("九阳真经", 16.9, 3), new Book("九阳神功", 11, 30),
new Book("九阳神掌", 16, 13), new Book("九阴白骨抓", 20, 6) };
for (int i = 0; i < book.length; i++) {
System.out.println("书名:" + book[i].getName() + " 价格:"
+ book[i].getPrice() + " 册数:" + book[i].getBook()
+ " 编号:" + book[i].getCount1());
}
System.out.println("总册数" + new Book().getTotalBook());
}
}
class Book {
private String name;// 书名
private static String number;// 编号
private double price;// 价格
private int book;// 册数
private static int totalbook;// 总册数
private static int count = 00000;// 编号
private int count1;
public Book() {
}
// 构造方法
public Book(String name, double price, int book) {
this.setName(name);
this.setPrice(price);
this.setBook(book);
totalbook = totalbook + book;
this.setCount();
count1=count;
}
// set方法
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setBook(int book) {
this.book = book;
}
private void setCount() {
count++;
}
// get方法
public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
public int getBook() {
return this.book;
}
public int getTotalBook() {
return this.totalbook;
}
public int getCount1() {
return this.count1;
}
}
- 面向对象(基础篇习题自己打的答案,错了请大胆的批评和教我怎么改(哈哈))
- 各种猥琐的Java笔试/面试题目的答案(这是我自己测试得出的答案,如有不对的欢迎批评指正)
- sql习题1(我的答案)
- 哈哈,我也准备建设自己的blog了!HAPPY!
- 我有自己的新家了,哈哈
- 各大it企业编程笔试题,答案自己做的,大家批评指正(更新中)
- 我的对象怎么了?
- 练习5 面向对象基础(含答案)
- 欢迎光临,请留下你宝贵的建议和批评,我会努力学习工作的!
- 面向对象习题(一)
- c++primer(第五版) 第十五章 面向对象程序设计习题答案
- 大学计算机基础书后习题答案(修改)
- Java基础习题(附答案)
- java使用 ArrayList 的增删改(Java面向对象基础练习)
- 前天题目的答案(这两天出去面试了,没上,哈哈)
- 我改的[放烟火],哈哈
- 哈哈,终于知道CSDN怎么改头像了
- Java面向对象程序设计--自己写的课后答案--
- 第四届蓝桥杯预赛试题之振兴中华
- 我写过的软件之UniversalTestForCCA
- 蓝桥杯 算法设计_6 分解质因数
- C#的排列组合类
- 虚拟化技术性能总结:Zones, KVM, Xen
- 面向对象(基础篇习题自己打的答案,错了请大胆的批评和教我怎么改(哈哈))
- Android初学常见问题
- PAT Basic Level 1013. 数素数(20)
- [Python]文件及文件夹相关操作
- 基于文法分析的简单计算器实现
- 自然语言处理中文 工具包
- String类常用用法总结
- poj1159 Palindrome
- ARP,RARP,ICMP,IGMP