设计雇员Employee类,记录雇员的情况,包括姓名、年薪、受雇时间,要求定义MyDate类作为受雇时间,其中包括工作的年、月、日,并用相应的方法对Employee类进行设置。编写测试类测试Employee类。
package liu;
public class Employee {
String name;
double money;
MyDate day;
public Employee(String name,double money,MyDate day){
this.name=name;
this.money=money;
this.day=day;
}
String hireYear(){
return day.getYear();
}
void print(){
System.out.println(name+" "+money+" "+hireYear());
}
}
class MyDate{
int year;
int month;
int day;
public MyDate(int year,int month,int day){
this.year=year;
this.month=month;
this.day=day;
}
String getYear(){
return(year+ "."+month+"."+day);
}
}
测试类:
package liu;
class Cshi {
public static void main(String[] args) {
Employee a=new Employee("小明",50000,new MyDate(2001,6,1));
Employee b=new Employee("小红",65201,new MyDate(2000,5,12));
a.print();
b.print();
}
}
创建一个代表天气对象的实体类Weather a) 拥有私有属性:日期date,地点location,风速windLevel,温度temperature,湿度humidity b) 拥有属性的get,set公有方法
package liu;
public class Weather {
String date;
String location;
String windLevel;
String temperature;
String humidity;
public Weather(String date,String location,String windLevel,String temperature,String humidity){
this.date=date;
this.location=location;
this.windLevel=windLevel;
this.temperature=temperature;
this.humidity=humidity;
}
String getdate() {
return date;
}
void setdate(String date) {
this.date=date;
}
String getLocation() {
return location;
}
void setLocation(String location) {
this.location = location;
}
String getWindLevel() {
return windLevel;
}
void setWindLevel(String windLevel) {
this.windLevel = windLevel;
}
String getTemperature() {
return temperature;
}
void setTemperature(String temperature) {
this.temperature = temperature;
}
String getHumidity() {
return humidity;
}
void setHumidity(String humidity) {
this.humidity = humidity;
}
}
定义一个箱子Box,有长、宽、高和编号,定义不同的构造方法来创建对象。
package liu;
public class Box {
int a;
int b;
int c;
String n;
Box(int a,int b,int c,String n){
this.a=a;
this.b=b;
this.c=c;
this.n=n;
}
void xx(){
System.out.println("长为:"+a+",宽为:"+b+",高为:"+c+",编号为:"+n);
}
int tj() {
int v=a*b*c;
return v;
}
}
定义Person类的有参构造方法,对成员变量进行初始化。
package liu;
public class Person {
String name;
int age;
String sex;
Person(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
}
调用Scanner类完成控制台接收键盘输入的整型数据,并输出。
package liu;
import java.util.Scanner;
public class Sc {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("请输入一个整数:");
int a=sc.nextInt();
System.out.println("输出:"+a);
}
}
}