练习1(基础):
(1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show() //将圆的半径、周长、面积输出到屏幕
package zuoye;
public class Circle {
private double Radius;
Circle(){
}
Circle(double r){
Radius=r;;
}
public double getArea(double Radius ) {
double area=3.14*Radius*Radius;
return area;
}
public double getPerimeter(double Radius ) {
double perimeter=3.14*2*Radius;
return perimeter;
}
public void show(){
System.out.println("圆的面积为:"+getArea(Radius));
System.out.println("圆的周长为:"+getPerimeter(Radius));
}
public static void main(String[] args){
Circle c=new Circle(4.0);
c.show();
}
}
练习1(基础):
定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
package zuoye;public class Student {
private int sNo;
private String sName;
private String sSex;
private int sAge;
private double sJava;
Student(int no, String name, String sex, int age, double java) {
sNo = no;
sName = name;
sSex = sex;
sAge = age;
sJava = java;
}
public int getsNo() {
return sNo;
}
public void setsNo(int sNo) {
this.sNo = sNo;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsSex() {
return sSex;
}
public void setsSex(String sSex) {
this.sSex = sSex;
}
public int getsAge() {
return sAge;
}
public void setsAge(int sAge) {
this.sAge = sAge;
}
public double getsJava() {
return sJava;
}
public void setsJava(double sJava) {
this.sJava = sJava;
}
}
package zuoye;
public class TestStudent {
public static void main(String[] args){
double ascore;
double maxScore;
Student s1=new Student(01,"xiaohong","女",18,90);
Student s2=new Student(02,"xiaoming","男",19,96);
Student s3=new Student(03,"xiaolan","女",18,99);
System.out.println(s1.getsNo()+" "+s1.getsName()+" "+s1.getsSex()+" "+s1.getsAge()+" "+s1.getsJava());
System.out.println(s2.getsNo()+" "+s2.getsName()+" "+s2.getsSex()+" "+s2.getsAge()+" "+s2.getsJava());
System.out.println(s3.getsNo()+" "+s3.getsName()+" "+s3.getsSex()+" "+s3.getsAge()+" "+s3.getsJava());
System.out.println("平均成绩为:"+(ascore=(s1.getsJava()+s2.getsJava()+s3.getsJava()))/3);
System.out.println("最高成绩"+Math.max(Math.max(s1.getsJava(), s2.getsJava()),s3.getsJava()));
System.out.println("最低成绩"+Math.min(Math.min(s1.getsJava(), s2.getsJava()),s3.getsJava()));
}
}