帮忙做JAVA题 很简单 分不是问题 在线急求答案QQ1261213498
1、定义一个“学生”类,包含的属性有“班级”、学号、姓名、性别、学习成绩;包含的方法有获取班级号,获取学号,获取姓名,获取性别,获取学习成绩。然后创建10个学生对象,输入...
1、 定义一个“学生”类,包含的属性有“班级”、学号、姓名、性别、学习成绩;包含的方法有获取班级号,获取学号,获取姓名,获取性别,获取学习成绩。然后创建10个学生对象,输入他们的成绩,并计算他们的平均成绩,最高分和最低分。
2、定义一个抽象类Shape,它有两个抽象的方法area()和perimeter(),分别表示求面积和周长。
子类:梯形类Trapezoid,实现抽象类Shape中求面积和周长的抽象方法。
子类:圆类Circle,实现抽象类中Shape中求面积和周长的抽象方法。 展开
2、定义一个抽象类Shape,它有两个抽象的方法area()和perimeter(),分别表示求面积和周长。
子类:梯形类Trapezoid,实现抽象类Shape中求面积和周长的抽象方法。
子类:圆类Circle,实现抽象类中Shape中求面积和周长的抽象方法。 展开
4个回答
展开全部
学生类:
package test;
public class Student {
public int cid;//班级号
public int sid;//学号
public String name;//姓名
public String sex;//性别
public int grade;//成绩
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
//测试类,求平均成绩 最高分和最低分
package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String args[]){
//创建测试样例
List<Student> list = addTest();
//定义一个总成绩
int count=0;
//定义一个最高成绩
int max=0;
//定义一个最低成绩
int min=0;
//定义一个最高成绩学生
String maxst=null;
//定义一个最低成绩学生
String minst=null;
for(int i =0 ;i<list.size();i++){
Student st = list.get(i);
if(st.getGrade()>max){
max=st.getGrade();
maxst=st.getName();
}
if(i==1){
min=st.getGrade();
minst=st.getName();
}else{
if(st.getGrade()<min){
min=st.getGrade();
minst=st.getName();
}
}
count+=st.getGrade();
}
//打印结果
System.out.println("最高成绩为"+maxst+":"+max+"分");
System.out.println("最低成绩为"+minst+":"+min+"分");
System.out.println("平均成绩为:"+count/list.size());
}
public static List<Student> addTest(){
//创建list存储10个学生对象
List<Student> list =new ArrayList<Student>();
for(int i =0;i<10;i++){
Student st = new Student();
//设置班级号
st.setCid(i);
//设置成绩
st.setGrade(Integer.parseInt(""+i+i));
//设置姓名
st.setName("test"+i);
//设置性别
if(i%2==0){
st.setSex("male");
}else{
st.setSex("female");
}
//设置sid
st.setSid(i+i);
list.add(st);
}
return list;
}
}
抽象类shape
package test;
public abstract class Shape {
public abstract float area();
public abstract float perimeter();
}
shape子类:
circle:
package test;
public class Circle extends Shape {
private static final float pi=3.14F;
private static final float banjing=123F;
@Override
public float area() {
float area=pi*banjing*banjing;
return area;
}
@Override
public float perimeter() {
float zhouchang=2*pi*banjing;
return zhouchang;
}
}
Trapezoid:
package test;
public class Trapezoid extends Shape {
//
@Override
public float area() {
// TODO Auto-generated method stub
return 0;
}
@Override
public float perimeter() {
// TODO Auto-generated method stub
return 0;
}
}
T形的实现你就自己仿照圆的那个做就行了.懒的写了
package test;
public class Student {
public int cid;//班级号
public int sid;//学号
public String name;//姓名
public String sex;//性别
public int grade;//成绩
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
}
//测试类,求平均成绩 最高分和最低分
package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String args[]){
//创建测试样例
List<Student> list = addTest();
//定义一个总成绩
int count=0;
//定义一个最高成绩
int max=0;
//定义一个最低成绩
int min=0;
//定义一个最高成绩学生
String maxst=null;
//定义一个最低成绩学生
String minst=null;
for(int i =0 ;i<list.size();i++){
Student st = list.get(i);
if(st.getGrade()>max){
max=st.getGrade();
maxst=st.getName();
}
if(i==1){
min=st.getGrade();
minst=st.getName();
}else{
if(st.getGrade()<min){
min=st.getGrade();
minst=st.getName();
}
}
count+=st.getGrade();
}
//打印结果
System.out.println("最高成绩为"+maxst+":"+max+"分");
System.out.println("最低成绩为"+minst+":"+min+"分");
System.out.println("平均成绩为:"+count/list.size());
}
public static List<Student> addTest(){
//创建list存储10个学生对象
List<Student> list =new ArrayList<Student>();
for(int i =0;i<10;i++){
Student st = new Student();
//设置班级号
st.setCid(i);
//设置成绩
st.setGrade(Integer.parseInt(""+i+i));
//设置姓名
st.setName("test"+i);
//设置性别
if(i%2==0){
st.setSex("male");
}else{
st.setSex("female");
}
//设置sid
st.setSid(i+i);
list.add(st);
}
return list;
}
}
抽象类shape
package test;
public abstract class Shape {
public abstract float area();
public abstract float perimeter();
}
shape子类:
circle:
package test;
public class Circle extends Shape {
private static final float pi=3.14F;
private static final float banjing=123F;
@Override
public float area() {
float area=pi*banjing*banjing;
return area;
}
@Override
public float perimeter() {
float zhouchang=2*pi*banjing;
return zhouchang;
}
}
Trapezoid:
package test;
public class Trapezoid extends Shape {
//
@Override
public float area() {
// TODO Auto-generated method stub
return 0;
}
@Override
public float perimeter() {
// TODO Auto-generated method stub
return 0;
}
}
T形的实现你就自己仿照圆的那个做就行了.懒的写了
追问
+qq1261213498 问些问题
追答
我不用QQ,你直接这里问就行了
展开全部
你好,按照你的要求我写了完整的代码,并给足了注释,可以直接运行
--------------------------------------------------------------------------------------------
第一题
--------------------------------------------------------------------------------------------
//学生类
class Stu {
int sclass;
int sno;
String sname;
boolean isMale;
int sscore;
public Stu(int sscore) {
super();
sclass = (int) (Math.random() * 10) + 1;
sno = (int) (Math.random() * 10) + 1;
sname = "学生" + (int) (Math.random() * 100 + 1);
isMale = (int) (Math.random() * 2) == 1;
this.sscore = sscore;
}
public int getSclass() {
return sclass;
}
public int getSno() {
return sno;
}
public String getSname() {
return sname;
}
public boolean isMale() {
return isMale;
}
public int getSscore() {
return sscore;
}
public static void main(String[] args) {
Stu[] s = new Stu[10];
int sum = 0;
int max = 0;
int min = 100;
for (int i = 0; i < 10; i++) {
s[i] = new Stu((int) (Math.random() * 101));
System.out.print(s[i].getSscore() + "\t");
sum += s[i].getSscore();
max = Math.max(max, s[i].getSscore());
min = Math.min(min, s[i].getSscore());
}
System.out.println("\n平均成绩:" + sum / 10.0);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
}
}
运行结果:
78 23 88 98 59 67 98 99 95 63
平均成绩:76.8
最高分:99
最低分:23
--------------------------------------------------------------------------------------------
第二题
--------------------------------------------------------------------------------------------
public class test2 {
public static void main(String[] args) {
Shape trapezoid = new Trapezoid(2, 6, 3, 5, 3);
System.out.println(trapezoid.toString());
System.out.println("梯形面积为:" + trapezoid.area());
System.out.println("梯形周长为:" + trapezoid.perimeter() + "\n");
Shape circle = new Circle(3);
System.out.println(circle.toString());
System.out.println("圆面积为:" + circle.area());
System.out.println("圆周长为:" + circle.perimeter());
}
}
// 抽象类
abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
// 梯形
class Trapezoid extends Shape {
int a, b;// 上,下
int c, d;// 左,右
int h;// 高
public Trapezoid(int a, int b, int c, int d, int h) {
super();
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.h = h;
}
public String toString() {
return "梯形 上边:" + a + "\t下边:" + b + "\t左边:" + c + "\t右边" + d + "\t高:"
+ h;
}
public double area() {
return (a + b) * h / 2.0;
}
public double perimeter() {
return a + b + c + d;
}
}
// 圆
class Circle extends Shape {
int r;// 半径
public Circle(int r) {
this.r = r;
}
public String toString() {
return "圆 半径:" + r;
}
public double area() {
return Math.PI * r * r;
}
public double perimeter() {
return 2 * Math.PI * r;
}
}
运行结果:
梯形 上边:2 下边:6 左边:3 右边5 高:3
梯形面积为:12.0
梯形周长为:16.0
圆 半径:3
圆面积为:28.274333882308138
圆周长为:18.84955592153876
--------------------------------------------------------------------------------------------
第一题
--------------------------------------------------------------------------------------------
//学生类
class Stu {
int sclass;
int sno;
String sname;
boolean isMale;
int sscore;
public Stu(int sscore) {
super();
sclass = (int) (Math.random() * 10) + 1;
sno = (int) (Math.random() * 10) + 1;
sname = "学生" + (int) (Math.random() * 100 + 1);
isMale = (int) (Math.random() * 2) == 1;
this.sscore = sscore;
}
public int getSclass() {
return sclass;
}
public int getSno() {
return sno;
}
public String getSname() {
return sname;
}
public boolean isMale() {
return isMale;
}
public int getSscore() {
return sscore;
}
public static void main(String[] args) {
Stu[] s = new Stu[10];
int sum = 0;
int max = 0;
int min = 100;
for (int i = 0; i < 10; i++) {
s[i] = new Stu((int) (Math.random() * 101));
System.out.print(s[i].getSscore() + "\t");
sum += s[i].getSscore();
max = Math.max(max, s[i].getSscore());
min = Math.min(min, s[i].getSscore());
}
System.out.println("\n平均成绩:" + sum / 10.0);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
}
}
运行结果:
78 23 88 98 59 67 98 99 95 63
平均成绩:76.8
最高分:99
最低分:23
--------------------------------------------------------------------------------------------
第二题
--------------------------------------------------------------------------------------------
public class test2 {
public static void main(String[] args) {
Shape trapezoid = new Trapezoid(2, 6, 3, 5, 3);
System.out.println(trapezoid.toString());
System.out.println("梯形面积为:" + trapezoid.area());
System.out.println("梯形周长为:" + trapezoid.perimeter() + "\n");
Shape circle = new Circle(3);
System.out.println(circle.toString());
System.out.println("圆面积为:" + circle.area());
System.out.println("圆周长为:" + circle.perimeter());
}
}
// 抽象类
abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
// 梯形
class Trapezoid extends Shape {
int a, b;// 上,下
int c, d;// 左,右
int h;// 高
public Trapezoid(int a, int b, int c, int d, int h) {
super();
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.h = h;
}
public String toString() {
return "梯形 上边:" + a + "\t下边:" + b + "\t左边:" + c + "\t右边" + d + "\t高:"
+ h;
}
public double area() {
return (a + b) * h / 2.0;
}
public double perimeter() {
return a + b + c + d;
}
}
// 圆
class Circle extends Shape {
int r;// 半径
public Circle(int r) {
this.r = r;
}
public String toString() {
return "圆 半径:" + r;
}
public double area() {
return Math.PI * r * r;
}
public double perimeter() {
return 2 * Math.PI * r;
}
}
运行结果:
梯形 上边:2 下边:6 左边:3 右边5 高:3
梯形面积为:12.0
梯形周长为:16.0
圆 半径:3
圆面积为:28.274333882308138
圆周长为:18.84955592153876
追问
+QQ1261213498 详细问一下
追答
你好,现在不能上QQ,不着急的话,晚上帮你解决
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一题
package test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import edu.emory.mathcs.backport.java.util.Collections;
public class Student implements Comparator<Object>{
private String classNumber;
private String studentId;
private String studentName;
private int studentSex;
private float studentScore;
public Student() {
}
public Student(String classNumber, String studentId, String studentName,
int studentSex, float studentScore) {
super();
this.classNumber = classNumber;
this.studentId = studentId;
this.studentName = studentName;
this.studentSex = studentSex;
this.studentScore = studentScore;
}
public String getClassNumber() {
return classNumber;
}
public void setClassNumber(String classNumber) {
this.classNumber = classNumber;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentSex() {
return studentSex;
}
public void setStudentSex(int studentSex) {
this.studentSex = studentSex;
}
public float getStudentScore() {
return studentScore;
}
public void setStudentScore(float studentScore) {
this.studentScore = studentScore;
}
@Override
public String toString() {
return "Student [classNumber=" + classNumber + ", studentId="
+ studentId + ", studentName=" + studentName
+ ", studentScore=" + studentScore + ", studentSex="
+ studentSex + "]";
}
private float calAverageScore(List<Student> stuList){
float averageScore=0f;
if(stuList!=null){
float sum=0f;
if(stuList.size()>=1){
for(Student stu:stuList){
sum+=stu.getStudentScore();
}
averageScore=sum/stuList.size();
}
}
return averageScore;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Student stuRun=new Student();
List<Student> stuList=new ArrayList<Student>();
for(int i=0;i<10;i++){
int sex=i%2==0?0:1;
Student stu=new Student("110101","1101000"+i+1,"[Student"+i+1+"]",sex,0);
float score=System.in.read();
stu.setStudentScore(score);
stuList.add(stu);
}
Collections.sort(stuList);
System.out.println("All the students' average score is: "+stuRun.calAverageScore(stuList));
System.out.println("The max score is: "+stuList.get(0).getStudentScore()+". And the student is: "+stuList.get(0).getStudentName());
System.out.println("The min score is: "+stuList.get(stuList.size()-1).getStudentScore()+". And the student is: "+stuList.get(stuList.size()-1).getStudentName());
}
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Student stu1=(Student)o1;
Student stu2=(Student)o2;
if(stu1.getStudentScore()-stu2.getStudentScore()>0){
return 1;
}
if(stu1.getStudentScore()-stu2.getStudentScore()<0){
return -1;
}else{
return 0;
}
}
}
第二题
Shape类
package test;
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
Trapezoid 类
package test;
public class Trapezoid extends Shape{
private float shangDi;
private float xiaDi;
private float leftSide;
private float rightSide;
private float height;
public float getShangDi() {
return shangDi;
}
public float getXiaDi() {
return xiaDi;
}
public float getLeftSide() {
return leftSide;
}
public float getRightSide() {
return rightSide;
}
public float getHeight() {
return height;
}
@Override
public double area() {
// TODO Auto-generated method stub
return ((shangDi+xiaDi)*height)/2;
}
@Override
public double perimeter() {
// TODO Auto-generated method stub
return shangDi+xiaDi+leftSide+rightSide;
}
}
Circle 类
package test;
public class Circle extends Shape{
private double radius;
public double getRadius() {
return radius;
}
@Override
public double area() {
// TODO Auto-generated method stub
return (Math.PI*radius*radius);
}
@Override
public double perimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*radius;
}
}
package test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import edu.emory.mathcs.backport.java.util.Collections;
public class Student implements Comparator<Object>{
private String classNumber;
private String studentId;
private String studentName;
private int studentSex;
private float studentScore;
public Student() {
}
public Student(String classNumber, String studentId, String studentName,
int studentSex, float studentScore) {
super();
this.classNumber = classNumber;
this.studentId = studentId;
this.studentName = studentName;
this.studentSex = studentSex;
this.studentScore = studentScore;
}
public String getClassNumber() {
return classNumber;
}
public void setClassNumber(String classNumber) {
this.classNumber = classNumber;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentSex() {
return studentSex;
}
public void setStudentSex(int studentSex) {
this.studentSex = studentSex;
}
public float getStudentScore() {
return studentScore;
}
public void setStudentScore(float studentScore) {
this.studentScore = studentScore;
}
@Override
public String toString() {
return "Student [classNumber=" + classNumber + ", studentId="
+ studentId + ", studentName=" + studentName
+ ", studentScore=" + studentScore + ", studentSex="
+ studentSex + "]";
}
private float calAverageScore(List<Student> stuList){
float averageScore=0f;
if(stuList!=null){
float sum=0f;
if(stuList.size()>=1){
for(Student stu:stuList){
sum+=stu.getStudentScore();
}
averageScore=sum/stuList.size();
}
}
return averageScore;
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Student stuRun=new Student();
List<Student> stuList=new ArrayList<Student>();
for(int i=0;i<10;i++){
int sex=i%2==0?0:1;
Student stu=new Student("110101","1101000"+i+1,"[Student"+i+1+"]",sex,0);
float score=System.in.read();
stu.setStudentScore(score);
stuList.add(stu);
}
Collections.sort(stuList);
System.out.println("All the students' average score is: "+stuRun.calAverageScore(stuList));
System.out.println("The max score is: "+stuList.get(0).getStudentScore()+". And the student is: "+stuList.get(0).getStudentName());
System.out.println("The min score is: "+stuList.get(stuList.size()-1).getStudentScore()+". And the student is: "+stuList.get(stuList.size()-1).getStudentName());
}
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Student stu1=(Student)o1;
Student stu2=(Student)o2;
if(stu1.getStudentScore()-stu2.getStudentScore()>0){
return 1;
}
if(stu1.getStudentScore()-stu2.getStudentScore()<0){
return -1;
}else{
return 0;
}
}
}
第二题
Shape类
package test;
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
}
Trapezoid 类
package test;
public class Trapezoid extends Shape{
private float shangDi;
private float xiaDi;
private float leftSide;
private float rightSide;
private float height;
public float getShangDi() {
return shangDi;
}
public float getXiaDi() {
return xiaDi;
}
public float getLeftSide() {
return leftSide;
}
public float getRightSide() {
return rightSide;
}
public float getHeight() {
return height;
}
@Override
public double area() {
// TODO Auto-generated method stub
return ((shangDi+xiaDi)*height)/2;
}
@Override
public double perimeter() {
// TODO Auto-generated method stub
return shangDi+xiaDi+leftSide+rightSide;
}
}
Circle 类
package test;
public class Circle extends Shape{
private double radius;
public double getRadius() {
return radius;
}
@Override
public double area() {
// TODO Auto-generated method stub
return (Math.PI*radius*radius);
}
@Override
public double perimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*radius;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-11-18
展开全部
分太少
追问
您想要几分?我可以加
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询