定义一个表示学生信息的类Student,要求如下:
(1)类Student的成员变量:
sNO 表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge()获得年龄;
getJava():获得Java 课程成绩
根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。(思路:对象数组)
package student;
public class Student {
private int sNO;
private String sName;
private String sSex;
private int sAge;
private int sJava;
public Student(int sNO, String sName, String sSex, int sAge, int sJava) {
super();
this.sNO = sNO;
this.sName = sName;
this.sSex = sSex;
this.sAge = sAge;
this.sJava = sJava;
}
public Student() {
super();
}
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 int getsJava() {
return sJava;
}
public void setsJava(int sJava) {
this.sJava = sJava;
}
@Override
public String toString() {
return "Student [sNO=" + sNO + ", sName=" + sName + ", sSex=" + sSex + ", sAge=" + sAge + ", sJava=" + sJava
+ "]";
}
}
package student;
import java.util.Random;
public class TestStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student[] stu=new Student[5];
Student s;
int grade=0,max=0,min=0,avg=0;
Random rand=new Random();
for(int i=0;i<5;i++) {
grade=rand.nextInt(100);
s=new Student(i+1,"px"+i,"m",18,grade);
stu[i]=s;
System.out.println(stu[i]);
avg+=stu[i].getsJava()/5;
if(max<stu[i].getsJava())
max=stu[i].getsJava();
}
min=stu[0].getsJava();
for(Student s1:stu) {
if(s1.getsJava()<min)
min=s1.getsJava();
}
System.out.println("max="+max+" min="+min+" avg="+avg);
}
}
定义3个重载方法max,第一个方法求两个int值中的最大值,第2个方法求三个int值中的最大值,第3个方法求四个int值中的最大值,并分别调用三个方法
package Max;
public class Max {
public static void main(String[] args) {
int max=0;
max=max(1,2);
System.out.println("max="+max);
max=max(1,2,3);
System.out.println("max="+max);
max=max(1,2,3,4);
System.out.println("max="+max);
}
public static int max(int a,int b) {
return a>b?a:b;
}
public static int max(int a,int b,int c) {
return (a>b?a:b)>c?(a>b?a:b):c;
}
public static int max(int a,int b,int c,int d) {
a=a>b?a:b;
c=c>d?c:d;
return a>c?a:c;
}
}
编写Addition类,该类中应包含一组实现两数相加运算的重载方法。实现加法运算的方法,应接受两个参数(即加数和被加数),方法将两个参数进行加法运算后,返回相加结果。考虑可能针对不同的数据类型进行计算,重载一组方法,包括整型、长整型、浮点型、双精度浮点型、还有字符串。在main方法中创建Addition类的实例,分别调用重载方法测试其效果。应将Addition类打入到包中,以com.hpepm.java为包名。
package com.hpepm.java;
public class Addition {
private Object i;
private Object j;
public int add(int i,int j) {
return i+j;
}
public long add(long i,long j) {
return i+j;
}
public double add(double i,double j) {
return i+j;
}
public float add(float i,float j) {
return i+j;
}
public String add(String i,String j) {
return i+j;
}
}
package com.hpepm.java;
public class TestAdd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Addition a=new Addition();
System.out.println(a.add(1, 2));
System.out.println(a.add(1, 2));
System.out.println(a.add(1.0, 2.0));
System.out.println(a.add("你好", "再见"));
}
}