成员变量基础练习
第1题
案例:
声明员工类Employee,包含属性:编号、姓名、年龄、薪资,
声明Test01测试类,并在main方法中,创建2个员工对象,并为属性赋值,并打印两个员工的信息。
public class Employee {
private int number;
private String name;
private int age;
private double salary;
public Employee(){
}
public Employee(int number,String name,int age,double salary){
this.number=number;
this.name=name;
this.age=age;
this.salary=salary;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber(){
return number;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setSalary(double salary){
this.salary=salary;
}
public double getSalary(){
return salary;
}
public void printInfo(){
System.out.println("编号是:"+number+",年龄是:"+age+"姓名是:"+name+"薪资是:"+salary);
}
}
package com.cwbjava.homework.homework_6.question_1;
public class Test {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setName("李白");
employee.setAge(18);
employee.setNumber(1);
employee.setSalary(2345.34);
employee.printInfo();
Employee employee1 = new Employee(2, "李清照", 18, 265.9);
employee1.printInfo();
}
}
第2题
案例:
声明一个日期类MyDate,包含属性:年、月、日
声明一个Test02测试类,并在main方法中,创建3个日期对象,一个是你的出生日期,一个是毕业的日期,并打印显示
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public MyDate() {
}
public int getYear() {
return year;
}
public void setYeat(int yeat) {
this.year = yeat;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year年=" + year +
", month月=" + month +
", day=日" + day +
'}';
}
}
public class Test1 { public static void main(String[] args) {
MyDate m1 = new MyDate();
m1.setYeat(1997);
m1.setMonth(6);
m1.setDay(14);
System.out.println(m1);
MyDate m2 = new MyDate(1997,3,14);
System.out.println(m2);
}
}
public class Test1 {
public static void main(String[] args) {
MyDate m1 = new MyDate();
m1.setYeat(1997);
m1.setMonth(6);
m1.setDay(14);
System.out.println(m1);
MyDate m2 = new MyDate(1997,3,14);
System.out.println(m2);
}
}
第3题
案例:
声明公民类Citizen,包含属性:姓名,生日,身份证号,其中姓名是String类型,生日是MyDate类型,身份证号也是String类型。
声明Test03测试类,在main方法中创建你们家庭成员的几个对象,并打印信息。
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public MyDate() {
}
public int getYear() {
return year;
}
public void setYeat(int yeat) {
this.year = yeat;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return "MyDate{" +
"year年=" + year +
", month月=" + month +
", day=日" + day +
'}';
}
}
/**
* 声明公民类Citizen,包含属性:姓名,生日,身份证号,其中姓名是String类型,生日是MyDate类型,
* 身份证号也是String类型。
* 声明Test03测试类,在main方法中创建你们家庭成员的几个对象,并打印信息。
*/
public class Citizen {
private String name;
private String id;
private MyDate birthday;
public Citizen(){
}
public Citizen(String name, String id, MyDate birthday) {
this.name = name;
this.id = id;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Citizen{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", birthday=" + birthday +
'}';
}
}
/**
* 声明Test03测试类,在main方法中创建你们家庭成员的几个对象,并打印信息。
*/
public class Test2 {
public static void main(String[] args) {
Citizen c1 = new Citizen();
MyDate myDate = new MyDate(1997,6,14);// 较为复杂的形式进行赋值
c1.setBirthday(myDate);
c1.setName("李白");
c1.setId("1348437583747");
System.out.println(c1);
MyDate myDate1 = new MyDate(1998,8,17);// 最简洁的形式进行赋值
Citizen c2 = new Citizen("黄埔", "1034758585", myDate);
System.out.println(c2);
Citizen c3 = new Citizen();// 最复杂的形式进行变量赋值
MyDate myDate2 = new MyDate();
myDate2.setYeat(1997);
myDate2.setMonth(5);
myDate2.setDay(29);
c3.setBirthday(myDate2);
c3.setName("王冰冰");
c3.setId("19834849589");
System.out.println(c3);
}
}
第4题
案例:
声明一个日期类MyDate,包含属性:年、月、日,并在MyDate类中声明几个方法:
1、boolean isLeapYear():判断当前日期的是闰年吗?
2、void set(int y, int m, int d):修改年,月,日为新日期
3、void puls(int years, int months, int days):加上years年,months月,days天后的日期
并在测试类Test04的main方法中创建对象,并调用测试
/**
* 声明一个日期类MyDate,包含属性:年、月、日,并在MyDate类中声明几个方法:
* <p>
* <p>
* <p>
* 2、void set(int y, int m, int d):修改年,月,日为新日期
* <p>
* 3、void puls(int years, int months, int days):加上years年,months月,days天后的日期
* <p>
* 并在测试类Test04的main方法中创建对象,并调用测试
*/
public class MyDate {
private int year;
private int day;
private int month;
public MyDate(int year, int mongth, int day) {
this.year = year;
this.day = mongth;
this.month = day;
}
public MyDate() {
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getAge() {
return day;
}
public void setAge(int age) {
this.day = age;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
@Override
public String toString() {
return "MyDate{" +
"year年=" + year +
", month月=" + month +
", age日=" + day +
'}';
}
// 1.、boolean isLeapYear():判断当前日期的是闰年吗?
public boolean isleapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {// 判断是闰年的标准
return true;
} else {
return false;
}
}
// 2、void set(int y, int m, int d):修改年,月,日为新日期
public void set(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
// 3、void puls(int years, int months, int days):加上years年,months月,days天后的日期
public void plus(int years,int months ,int days){
this.year+=years;
this.month+=months;
this.day+=days;
while (day>28){// 从后面进行判断,此时天数有四种可能31 30 28 29
// 是大月
if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10||this.month==12){
day=day-31;
month++;
// 是是二月
}else if(month==2){
// 是闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {// 判断是闰年的标准
day=day-28;
month++;
// 是平年
} else {
day=day-29;
month++;
}
//是大月
}else if(this.month==4||this.month==6||this.month==9||this.month==11){
day=days-30;
month++;
}
}
// 月份是否增加
while (month>12){
month=months-12;
year++;
}
}
}
public class Test {
public static void main(String[] args) {
MyDate myDate = new MyDate(1997, 6, 25);
System.out.println(myDate);
boolean isleapYear = myDate.isleapYear(2000);// 判断是否是闰年
System.out.println(isleapYear);
myDate.set(1888,3,5);
System.out.println(myDate);
myDate.plus(1,2,3);
System.out.println(myDate);
}
第5题
案例:
声明一个三角形类Triangle,包含属性:a,b,c,表示三条边,包含几个方法:
1、boolean isRightTriangle():判断是否是一个直角三角形
2、boolean isIsoscelesTriangle():判断是否是一个等腰三角形
3、boolean isEquilateralTriangle():判断是否是一个等边三角形
4、double getArea():根据三条边,用海伦公式求面积
5、double getLength():求周长
并在测试类Test05的main方法中调用测试
/**
* 声明一个三角形类Triangle,包含属性:a,b,c,表示三条边,包含几个方法:
*
* 1、boolean isRightTriangle():判断是否是一个直角三角形
*
* 2、boolean isIsoscelesTriangle():判断是否是一个等腰三角形
*
* 3、boolean isEquilateralTriangle():判断是否是一个等边三角形
*
* 4、double getArea():根据三条边,用海伦公式求面积
*
* 5、double getLength():求周长
*
* 并在测试类Test05的main方法中调用测试
*/
public class Triangle {
// 声明一个三角形类Triangle,包含属性:a,b,c,表示三条边,包含几个方法:
private double a;
private double b;
private double c;
public Triangle() {
}
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
@Override
public String toString() {
return "Triangle{" +
"a=" + a +
", b=" + b +
", c=" + c +
'}';
}
//1、boolean isRightTriangle():判断是否是一个直角三角形
public boolean isRightTriangle(){
if(a*a+b*b==c*c||a*a+c*c==b*b||c*c+b*b==a*a){
return true;
}
return false;
}
//
//2、boolean isIsoscelesTriangle():判断是否是一个等腰三角形
public boolean idIsoscelesTriangle(){
if(a==b||a==c||b==c){
return true;
}
return false;
}
//
//3、boolean isEquilateralTriangle():判断是否是一个等边三角形
public boolean isEquilateralTriangle(){
if(a==b&&a==c){
return true;
}
return false;
}
//
//4、double getArea():根据三条边,用海伦公式求面积表达式为:S=√p(p-a)(p-b)(p-c),
public double getArea(){
double p=(a+b+c)/2;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
//
//5、double getLength():求周长
public double getLength(){
return a+b+c;
}
//
// 并在测试类Test05的main方法中调用测试
}
public class Test2 { public static void main(String[] args) {
Triangle t1 = new Triangle(10 ,10, 15);
System.out.println(t1);
boolean rightTriangle = t1.isRightTriangle();// 直角三角形
System.out.println("rightTriangle = " + rightTriangle);
boolean idIsoscelesTriangle = t1.idIsoscelesTriangle();// 等腰直角三角形
System.out.println("idIsoscelesTriangle = " + idIsoscelesTriangle);
boolean equilateralTriangle = t1.isEquilateralTriangle();// 判断是不是等边三角形
System.out.println("equilateralTriangle = " + equilateralTriangle);
double area = t1.getArea();// 求面积
System.out.println("area = " + area);
double length = t1.getLength();// 求周长
System.out.println("length = " + length);
}
}
第6题
案例:
声明一个数学计算工具类MathTools,包含如下方法:
1、int add(int a, int b):求a+b
2、int subtract(int a,int b):求a-b
3、int mutiply(int a, int b):求a*b
4、int divide(int a, int b):求a/b
5、int remainder(int a, int b):求a%b
6、int max(int a, int b):求a和b中的最大值
7、int min(int a, int b):求a和b中的最小值
8、boolean equals(int a, int b):判断a和b是否相等
9、boolean isEven(int a):判断a是否是偶数
10、boolean isPrimeNumer(int a):判断a是否是素数
11、int round(double d):返回d的四舍五入后的整数值
声明一个Test06测试类,并在main方法中调用测试
/**
* ## 第6题
* <p>
* 案例:
* <p>
* 声明一个数学计算工具类MathTools,包含如下方法:
* <p>
* 1、int add(int a, int b):求a+b
* <p>
* 2、int subtract(int a,int b):求a-b
* <p>
* 3、int mutiply(int a, int b):求a*b
* <p>
* 4、int divide(int a, int b):求a/b
* <p>
* 5、int remainder(int a, int b):求a%b
* <p>
* 6、int max(int a, int b):求a和b中的最大值
* <p>
* 7、int min(int a, int b):求a和b中的最小值
* <p>
* 8、boolean equals(int a, int b):判断a和b是否相等
* <p>
* 9、boolean isEven(int a):判断a是否是偶数
* <p>
* 10、boolean isPrimeNumer(int a):判断a是否是素数
* <p>
* 11、int round(double d):返回d的四舍五入后的整数值
*/
public class MathTool {
private int a;
private int b;
public MathTool(int a, int b) {
this.a = a;
this.b = b;
}
public MathTool() {
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int add(int a, int b) {
return a + b;
}
@Override
public String toString() {
return "MathTool{" +
"a=" + a +
", b=" + b +
'}';
}
// ## 第6题
//
//案例:
//
// 声明一个数学计算工具类MathTools,包含如下方法:
//
//1、int add(int a, int b):求a+b
//
//2、int subtract(int a,int b):求a-b
public int subtract(int a, int b) {
return a - b;
}
//
//3、int mutiply(int a, int b):求a*b
public int mutiply(int a, int b) {
return a * b;
}
//
//4、int divide(int a, int b):求a/b
public int divide(int a, int b) {
return a / b;
}
//
//5、int remainder(int a, int b):求a%b
public int remainder(int a, int b) {
return a % b;
}
//
//6、int max(int a, int b):求a和b中的最大值
public int max(int a, int b) {
return a > b ? a : b;
}
//
//7、int min(int a, int b):求a和b中的最小值
public int min(int a, int b) {
return a > b ? b : a;
}
//
//8、boolean equals(int a, int b):判断a和b是否相等
public boolean equals(int a, int b) {
if (a == b) {
return true;
}
return false;
}
//
//9、boolean isEven(int a):判断a是否是偶数
public boolean isEvex(int a) {
if (a % 2 == 0) {
return true;
}
return false;
}
//
//10、boolean isPrimeNumer(int a):判断a是否是素数
public boolean isPrimeNumer(int a) {
boolean flag = true;
for (int i = 2; i <= Math.sqrt(a); i++) {// 素数判断可以是一半也可以是根号数是素数的数学性质
if (a % i == 0) {
flag=false;
return flag;
}
}
return flag;
}
//
//11、int round(double d):返回d的四舍五入后的整数值
public int round(double d){
return (int)d;
}
}
public class Test {
public static void main(String[] args) {
MathTool tool = new MathTool();
int add = tool.add(12, 30);
System.out.println("add = " + add);
int subtract = tool.subtract(10, 34);
System.out.println("subtract = " + subtract);
int mutiply = tool.mutiply(10, 29);
System.out.println("mutiply = " + mutiply);
int divide = tool.divide(10, 2);
System.out.println("divide = " + divide);
int remainder = tool.remainder(9, 2);
System.out.println("remainder = " + remainder);
int max = tool.max(10, 20);
System.out.println("max = " + max);
int min = tool.min(20, 30);
System.out.println("min = " + min);
boolean equals = tool.equals(10, 10);
System.out.println("equals = " + equals);
boolean evex = tool.isEvex(20);
System.out.println("evex = " + evex);
boolean primeNumer = tool.isPrimeNumer(9);
System.out.println("primeNumer = " + primeNumer);
int round = tool.round(20.3);
System.out.println("round = " + round);
}
}
第7题
案例:
声明一个数组管理工具类MyArrays,包含如下方法:
1、void sort(int[] arr):可以为任意一维整型数组arr实现从小到大排序
2、int indexOf(int[] arr, int value):可以在任意一维整型数组arr中查找value值的下标,如果不存在返回-1
3、int[] copy(int[] arr, int len):可以实现从任意一维数组arr中复制一个新数组返回,新数组的长度为len,从arr[0]开始复制
声明一个Test07测试类,并在main方法中调用测试
import java.util.Arrays;
public class MyArrays {
private int[]arr;
public MyArrays(int[] arr) {
this.arr = arr;
}
public MyArrays() {
}
public int[] getArr() {
return arr;
}
public void setArr(int[] arr) {
this.arr = arr;
}
@Override
public String toString() {
return "MyArrays{" +
"arr=" + Arrays.toString(arr) +
'}';
}
// ## 第7题
//
//案例:
//
// 声明一个数组管理工具类MyArrays,包含如下方法:
//
//1、void sort(int[] arr):可以为任意一维整型数组arr实现从小到大排序
public void sort(int[]arr){
boolean flag=true;
l:for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {// 防止下标越界,此外循环一次就可以少对比一次
if (arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
flag=false;
}
if (flag==true){// 如果没有进行交换此时就直接结束循环
break l;
}
}
}
}
//
//2、int indexOf(int[] arr, int value):可以在任意一维整型数组arr中查找value值的下标,如果不存在返回-1
public int indexOf(int[]arr,int value){
int index=-1;
for (int i = 0; i < arr.length; i++) {
if ((arr[i]==value)){
index=i;
return index;
}
}
return index;
}
//
//3、int[] copy(int[] arr, int len):可以实现从任意一维数组arr中复制一个新数组返回,新数组的长度为len,从arr[0]开始复制
//
public int[] copy(int[]arr,int len){
int []str=new int[len];
for (int i = 0; i < len; i++) {
str[i]=arr[i];
}
return str;
}
// 声明一个Test07测试类,并在main方法中调用测试
}
package com.cwbjava.homework.homework_6.question_7;
public class Test {
public static void main(String[] args) {
int[]arr=new int[]{6,5,4,3,2,1};
MyArrays myArrays = new MyArrays(arr);
System.out.println(myArrays);
myArrays.sort(arr);
System.out.println(myArrays);
int i = myArrays.indexOf(arr, 4);
System.out.println("i = " + i);
int[] copy = myArrays.copy(arr,6);
for (int i1 = 0; i1 < copy.length; i1++) {
System.out.println(copy[i1]);
}
}
}
第8题
案例:
声明一个常识工具类DateCommonsTools,包含如下方法:
1、String getWeekName(int week):根据星期值,返回对应的英语单词
2、String getMonthName(int month):根据月份值,返回对应的英语单词
3、int getTotalDaysOfMonth(int year, int month):返回某年某月的总天数
4、int getTotalDaysOfYear(int year):获取某年的总天数
5、boolean isLeapYear(int year):判断某年是否是闰年
声明一个Test08测试类,并在main方法中调用测试
// 案例:
//
// 声明一个常识工具类DateCommonsTools,包含如下方法:
public class DateCommonsTools {
String getWeekName(int week) {
switch (week) {
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
case 7:
return "Sunday";
default:
return "你输入的星期错误!!!!!!";
}
}
//2、String getMonthName(int month):根据月份值,返回对应的英语单词
public String gerMonthName(int month){
switch (month){
case 1:
return "january";
case 2:
return "february";
case 3:
return "march";
case 4:
return "april";
case 5:
return "may";
case 6:
return "june";
case 7:
return "july";
case 8:
return "august";
case 9:
return "september";
case 10:
return "october";
case 11:
return "novemeber";
case 12:
return "december";
default:
return "你输入的月份错误";
}
}
//3、int getTotalDaysOfMonth(int year, int month):返回某年某月的总天数
public int getTotalDaysOfMonth(int year,int month){
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
} else if(month==2){
if (year%4==0&&year%100!=0||year%400==0){
return 28;
}else {
return 29;
}
}else if(month==4||month==6||month==9||month==11){
return 30;
}else {
return -1;
}
}
//
//4、int getTotalDaysOfYear(int year):获取某年的总天数
public int getTotalDaysOfYear(int year){
if (year%4==0&&year%100!=0||year%400==0){
return 364;
}
return 365;
}
//
//5、boolean isLeapYear(int year):判断某年是否是闰年
boolean isLeapYear(int year){
if(year%4==0&&year%100!=0||year%400==0){
return true;
}
return false;
}
//
// 声明一个Test08测试类,并在main方法中调用测试
}
public class Test1 {
public static void main(String[] args) {
DateCommonsTools dateCommonsTools = new DateCommonsTools();
String weekName = dateCommonsTools.getWeekName(4);
System.out.println("weekName = " + weekName);
String s = dateCommonsTools.gerMonthName(9);
System.out.println("s = " + s);
int totalDaysOfYear = dateCommonsTools.getTotalDaysOfYear(1997);
System.out.println("totalDaysOfYear = " + totalDaysOfYear);
int totalDaysOfMonth = dateCommonsTools.getTotalDaysOfMonth(1997, 6);
System.out.println("totalDaysOfMonth = " + totalDaysOfMonth);
boolean leapYear = dateCommonsTools.isLeapYear(2000);
System.out.println("leapYear = " + leapYear);
}
}