编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息。
class Address {
private String country;
private String province;
private String city;
private String street;
private String zipcode;
public Address() {};
public Address (String country, String province, String city, String street, String zipcode) {
this.country = country;
this.province = province;
this.city = city;
this.street = street;
this.zipcode = zipcode;
}
public String getInfo () {
return "国家:" + this.country + "、省份:" + this.province +"、城市:" + this.city + "、街道:" + this.street + "、邮编:" + this.zipcode;
}
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 setZipcode (String zipcode) {
this.zipcode = zipcode;
}
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 String getZipcode () {
return this.zipcode;
}
}
public class JavaStudy {
public static void main (String [] args) {
System.out.println(new Address("中国", "河北省", "石家庄", "和平路", "050000").getInfo());
}
}
定义一个代表员工的Employee类,员工属性包括编号、姓名、基本薪水、薪水增长率,包括计算薪水增长额及计算后的工资总额的操作方法。
class Employee {
private long empno;
private String empname;
private double salary;
private double rate;
public Employee () {}
public Employee (long empno, String empname, double salary, double rate) {
this.empno = empno;
this.empname = empname;
this.salary = salary;
this.rate = rate;
}
public double salaryIncValue () {
return this.salary * this.rate;
}
public double salaryIncResult () {
this.salary = this.salary * (1 + this.rate);
return this.salary;
}
public String getInfo () {
return "员工编号:" + this.empno + "、员工姓名:" + this.empname + "、基本工资:" + this.salary + "、工资增长率:" + this.rate;
}
public void setEmpno (long empno) {
this.empno = empno;
}
public void setEmpname (String empname) {
this.empname = empname;
}
public void setSalary (double salary) {
this.salary = salary;
}
public void setRate (double rate) {
this.rate = rate;
}
public long getEmpno () {
return this.empno;
}
public String getEmpname () {
return this.empname;
}
public double getSalary () {
return this.salary;
}
public double getRate () {
return this.rate;
}
}
public class JavaStudy {
public static void main (String [] args) {
Employee emp = new Employee(9527L, "吴志明", 2000.0, 0.5);
System.out.println(emp.getInfo());
System.out.println("工资增长额度:" + emp.salaryIncValue());
System.out.println("上调后工资额度:" + emp.salaryIncResult());
}
}
设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出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 (String name) {
this.name = name;
}
public void setColor (String color) {
this.color = color;
}
public void setAge (int age) {
this.age = age;
}
public String getName () {
return this.name;
}
public String getColor () {
return this.color;
}
public int getAge () {
return this.age;
}
}
public class JavaStudy {
public static void main (String [] args) {
Dog dog = new Dog("奶油", "黄色", 2);
System.out.println(dog.getInfo());
}
}
构造一个银行账户类,类的内容包含如下内容:
数据成员的账户名称、用户的账户余额(private数据类型)
方法包括开会(设置账户名称和余额),利用构造方法完成
查询余额
class Account {
private String name;
private double balance;
public Account () {}
public Account (String name) {
this(name, 0.0);
}
public Account (String name, double balance) {
this.name = name;
this.balance = balance;
}
public String getInfo () {
return "账户名称:" + this.name + "、余额:" + this.balance;
}
public void setName (String name) {
this.name = name;
}
public void setBalance (double balance) {
this.balance = balance;
}
public String getName () {
return this.name;
}
public double getBalance () {
return this.balance;
}
}
public class JavaStudy {
public static void main (String [] args) {
Account account = new Account("小明", 2000.0);
System.out.println(account.getInfo());
System.out.println("小明当前余额:" + account.getBalance());
Account account_new = new Account("小红");
System.out.println(account_new.getInfo());
System.out.println("小红当前余额:" + account_new.getBalance());
}
}
设计一个表示用户的User类,类中的变量有用户名、口令和表示用户个数的变量,定义类的三个构造方法(无参、为用户名赋值、为用户名和口令赋值)、获取和设置口令的方法和返回类信息的方法。
class User {
private String uid;
private String password;
private static int count = 0;
public User() {
this("NOID", "123123");
}
public User(String uid) {
this(uid, "123123");
}
public User(String uid, String password) {
this.uid = uid;
this.password = password;
count++;
}
public static int getCount() {
return count;
}
public String getInfo () {
return "用户名:" + this.uid + "、口令:" + this.password;
}
public void setUid(String uid) {
this.uid = uid;
}
public void setPassword(String password) {
this.password = password;
}
public String getUid() {
return this.uid;
}
public String getPassword() {
return this.password;
}
}
public class JavaStudy {
public static void main(String [] args) {
User user_no_param = new User();
User user_has_one_param = new User("小明");
User user_has_two_param = new User("小红", "123456");
System.out.println(user_no_param.getInfo());
System.out.println(user_has_one_param.getInfo());
System.out.println(user_has_two_param.getInfo());
System.out.println("用户个数:" + User.getCount());
}
}
设计一个图书类,其数据成员为书名、编号(利用静态变量实现自动编号)、书价并拥有静态数据成员册数,记录图书的总册数,在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义多个对象,并求出总册数。
class Book {
private int bid;
private String title;
private double price;
private static int count = 0;
public Book() {}
public Book(String title, double price) {
this.bid = count + 1;
this.title = title;
this.price = price;
count++;
}
public static int getCount() {
return count;
}
public String getInfo() {
return "图书编号:" + this.bid + "、图书名称:" + this.title + "、图书价格:" + this.price;
}
public void setBid(int bid) {
this.bid = bid;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrice(double proce) {
this.price = price;
}
public int getBid() {
return this.bid;
}
public String getTitle() {
return this.title;
}
public double getPrice() {
return this.price;
}
}
public class JavaStudy {
public static void main(String [] args) {
Book book_java = new Book("Java", 19.9);
Book book_js = new Book("JS", 9.9);
System.out.println(book_java.getInfo());
System.out.println(book_js.getInfo());
System.out.println("图书总册书:" + Book.getCount());
}
}
数组排序,按照我们想要的顺序排序,当前案例为升序
class ArrayUtil {
public static void sort(int data[]) {
for(int x = 0; x < data.length; x++) {
for(int y = 0; y < data.length - x - 1; y ++) {
if(data[y] > data[y + 1]) {
int temp = data[y];
data[y] = data[y + 1];
data[y + 1] = temp;
}
}
}
}
public static void printlnArray(int temp []) {
for(int x : temp) {
System.out.print(x + "、");
}
}
}
public class ArrayDemo {
public static void main (String args []) {
int data [] = new int [] {8, 9, 0, 2, 3, 5, 10, 7, 6, 1};
ArrayUtil.sort(data);
ArrayUtil.printlnArray(data);
}
}
数组反转,前后转置处理,即:首尾交换
class ArrayUtil {
public static void reverse(int data[]) {
int center = data.length / 2;
int head = 0;
int tail = data.length - 1;
for(int x = 0; x < center; x++) {
int temp = data[head];
data[head] = data[tail];
data[tail] = temp;
head++;
tail--;
}
}
public static void printlnArray(int temp []) {
for(int x : temp) {
System.out.print(x + "、");
}
}
}
public class ArrayDemo {
public static void main (String args []) {
int data [] = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9};
ArrayUtil.reverse(data);
ArrayUtil.printlnArray(data);
}
}