2. Firstly, design a class Person.
1.1) The class contains three private fields: String name, int born, String country.
2.2) A constructor with three arguments that construct a Person object with the specified name, born and country.
3.3) All the get and set method for all fields.
4.4) A toString method to print information of Person as following types Person [name=Mia, born=1978, country=Peru]
Person [name=Ziyi, born=1979, country=Japan]
Create a class Exercise2, which contains the main method. In the class we need read two files person.txt and cn_person.txt respectively. Then we should convert the type of each line in the two files to an object of Person. If a file has Chinese characters, we should use “UTF-8” as the encoding format.
Reader reader = new InputStreamReader(new FileInputStream(path),"UTF-8");
Finally, writing all objects of Person into all.txt file with a format as toString method we defined in Person class.
Tips: The data in person.txt and cn_person.txt we provided are just sample data, actually the test data maybe much larger than them and you won’t know how many lines in each one.
Sample output in console window
Begin to read person.txt
Finish Reading
Begin to read cn_person.txt
Finish Reading
Begin to write all.txt
Finish Writing
Sample output in all.txt
Person [name=Mia, born=1978, country=Peru] Person [name=Ziyi, born=1979, country=Japan] Person [name=Chris, born=1971, country=France] Person [name=Joe, born=1976, country=Japan] Person [name=陈坤, born=1976, country=中国] Person [name=周迅, born=1974, country=中国] Person [name=赵薇, born=1976, country=中国]
4. According to the User class given to you, design two another sub classes
named AdminUserand OrdinaryUser
Create a class named OrdinaryUser extended from User
a. Add one instance variable balance (int),which means how many
money the ordinary user has in his account.
b. Create a two-argument constructor(username password). The original
value of balance can be 0.
c. Adding a method named pay(), which is used for pay the fees for
system. When an ordinary user paid some money, his balance will be reduced.
public void pay(intmoney)
d. Adding a method named recharge(), which is used for recharging the
fees. When an ordinary user recharged some money, his balance will
public void recharge(intmoney)
e. Override toString() method by adding balance information, and not
missing the other information in super class.
Create another classnamed AdminUser extended from User
1.We also need to create constructor and toString()method as the
OrdinaryUser designed .
2.Adding a method named resetPassword(), from which an object of
be increased.
OrdinaryUser should be passed into this method, and then we should change the password of this ordinary user to “123456”
public void resetPassword(OrdinaryUserordinaryUser)
According to the main method
public static void main(String args[]){
AdminUseru1=new AdminUser("Bob","aaabbb"); OrdinaryUseru2=new OrdinaryUser("Emma","aaadddd"); System.out.println(u1.toString()); System.out.println(u2.toString()); u1.resetPassword(u2);
u2.recharge(1000);
u2.pay(200);
System.out.println(u2);
}
Sample output:
AdminUser [userId=User1, username=Bob, password=aaabbb]
OrdinaryUser [balance=0,[userId=User2, username=Emma, password=aaadddd]]
OrdinaryUser [balance=800,[userId=User2, username=Emma, password=123456]]
If we change this line tou2.pay(1200);
Sample output:
AdminUser [userId=User1, username=Bob, password=aaabbb]
OrdinaryUser [balance=0,[userId=User2, username=Emma, password=aaadddd]] balance is not enough
OrdinaryUser [balance=1000,[userId=User2, username=Emma, password=123456]]
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。