一、语法练习
代码实现:
Circle类:
public class Circle
{
final double Pi=3.14;
double r;//圆的半径
public Circle(){}
public Circle(double r)
{
this.r = r;
}
public double getR()
{
return r;
}
public void setR(double r)
{
this.r = r;
}
//计算圆的周长
public void showArea(double r){
double S=Pi*r*r;
System.out.println("圆的面积是:"+S);
}
//计算圆的面积
public void showPerimeter(double r){
double C=2*Pi*r;
System.out.println("圆的周长是:"+C);
}
}
TestCircle类:
public class TestCircle
{
public static void main(String[] args)
{
Circle circle=new Circle();
circle.showArea(2.5);
circle.showPerimeter(4);
}
}
运行截图:
二、语法练习
代码实现:
MyDate类:
public class MyDate
{
int year;
int month;
int day;
public MyDate(){}
public MyDate(int year, int month, int day)
{
this.year = year;
this.month = month;
this.day = day;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
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;
}
//打印日期方法
public void showDate(int year,int month,int day){
System.out.println("日期是:"+year+"年"+month+"月"+day+"日");
}
//判断年饭是否为闰年方法
public void isBi(int year){
if (year%4==0&&year%100!=0) {
System.out.println(year+"是闰年");
}else if (year%400==0&&year%100==0){
System.out.println(year+"是闰年");
}else {
System.out.println(year+"不是闰年");
}
}
}
TestMyDate类:
public class TestMyDate
{
public static void main(String[] args)
{
MyDate myDate= new MyDate();
myDate.showDate(1900,1,1);
myDate.isBi(1900);
}
}
运行截图:
三、语法练习
代码实现:
Card类:
public class Card{
{
String point;
String suit;
public Card(){}
public Card(String point, String suit)
{
this.point = point;
this.suit = suit;
}
public String getPoint()
{
return point;
}
public void setPoint(String point)
{
this.point = point;
}
public String getSuit()
{
return suit;
}
public void setSuit(String suit)
{
this.suit = suit;
}
//打印牌面信息
public void showCard( String suit,String point){
System.out.println(suit+""+point);
}
}
TestCard类:
public class TestCard
{
public static void main(String[] args)
{
Card card= new Card();
System.out.print("牌面信息是:");
card.showCard("黑桃","A");
}
}
运行截图:
四、语法练习
代码实现:
Manager类:
public class Manager
{
String ManagerName;//管理人员姓名
String ManagerNumber;//管理人员工号
int ManagerSalary;//管理人员薪资
double BasePay;//基本工资
double award;//奖金
public Manager(){}
public Manager(String managerName, String managerNumber, int managerSalary)
{
ManagerName = managerName;
ManagerNumber = managerNumber;
ManagerSalary= managerSalary;
}
public String getManagerName()
{
return ManagerName;
}
public void setManagerName(String managerName)
{
ManagerName = managerName;
}
public String getManagerNumber()
{
return ManagerNumber;
}
public void setManagerNumber(String managerNumber)
{
ManagerNumber = managerNumber;
}
public int getManagerSalary()
{
return ManagerSalary;
}
public void setManagerSalary(int managerSalary)
{
ManagerSalary = managerSalary;
}
public double getBasePay()
{
return BasePay;
}
public void setBasePay(double basePay)
{
BasePay = basePay;
}
public double getAward()
{
return award;
}
public void setAward(double award)
{
this.award = award;
}
//打印姓名,工号信息
public void intro(String managerName,String managerNumber){
System.out.println("经理姓名:"+managerName+"\n"+"工号:"+managerNumber);
}
//打印薪资信息
public void showSalary( double BasePay,double award){
ManagerSalary=(int)(BasePay+award);
System.out.println("基本工资为:"+BasePay+"奖金为:"+award);
}
//打印工作信息
public void work(){
System.out.println("正在努力的做着管理工作,分配任务,检查员工提交上来的代码.....");
}
}
Coder类:
public class Coder
{
String CoderName;//员工姓名
String CoderNumber;//员工工号
int CoderBasePay;//员工基本工资
public Coder(){}
public Coder(String coderName, String coderNumber, int coderBasePay)
{
CoderName = coderName;
CoderNumber = coderNumber;
CoderBasePay = coderBasePay;
}
public String getCoderName()
{
return CoderName;
}
public void setCoderName(String coderName)
{
CoderName = coderName;
}
public String getCoderNumber()
{
return CoderNumber;
}
public void setCoderNumber(String coderNumber)
{
CoderNumber = coderNumber;
}
public int getCoderBasePay()
{
return CoderBasePay;
}
public void setCoderBasePay(int coderBasePay)
{
CoderBasePay = coderBasePay;
}
//打印姓名,工号信息
public void intro(String CoderName,String CoderNumber){
System.out.println("员工姓名:"+CoderName+"\n"+"工号:"+CoderNumber);
}
//打印薪资信息
public void showSalary(int CoderBasePay ){
System.out.println("基本工资为:"+CoderBasePay+"奖金为:无");
}
//打印工作信息
public void work(){
System.out.println("正在努力的写的代码.....");
}
}
Test类:
public class Test
{
public static void main(String[] args)
{
Manager manager = new Manager();
manager.intro("James","9527");
manager.showSalary(15000,3000);
manager.work();
System.out.println("=========");
Coder coder = new Coder();
coder.intro("Kode","0025");
coder.showSalary(10000);
coder.work();
}
}
运行截图:
五、语法练习
代码实现:
Teacher类:
public class Teacher
{
String TeacherName;//教师姓名
int TeacherAge;//教师年龄
String content;//讲课内容
public Teacher(){}
public Teacher(String teacherName, int TeacherAge, String content)
{
TeacherName = teacherName;
this.TeacherAge = TeacherAge;
this.content = content;
}
public String getTeacherName()
{
return TeacherName;
}
public void setTeacherName(String teacherName)
{
TeacherName = teacherName;
}
public int TeacherAge()
{
return TeacherAge;
}
public void setTeacherAge(int TeacherAge)
{
this.TeacherAge = TeacherAge;
}
public String getContent()
{
return content;
}
public void setContent(String content)
{
this.content = content;
}
//吃饭方法
public void eat(int TeacherAge,String TeacherName){
System.out.println("年龄为"+TeacherAge+"的"+TeacherName+"正在吃饭....");
}
//讲课方法
public void teach(int TeacherAge,String TeacherName,String content){
System.out.println("年龄为"+TeacherAge+"的"+TeacherName+"正在亢奋的讲着"+content+"的知识");
}
}
Student类:
public class Student
{
String StudentName;//学生姓名
int StudentAge;//学生年龄
String content;//学习内容
public Student(){}
public Student(String studentName, int studentAge, String content)
{
StudentName = studentName;
StudentAge = studentAge;
this.content = content;
}
public String getStudentName()
{
return StudentName;
}
public void setStudentName(String studentName)
{
StudentName = studentName;
}
public int getStudentAge()
{
return StudentAge;
}
public void setStudentAge(int studentAge)
{
StudentAge = studentAge;
}
public String getContent()
{
return content;
Test1类:
public class Test1
{
public static void main(String[] args)
{
Teacher teacher= new Teacher();
teacher.eat(30,"周老师");
teacher.teach(30,"周老师","java");
Student student = new Student();
student.eat(18,"韩同学");
student.study(18,"韩同学","java");
}
}