(1.编写一个计算各种形状的面积和周长的程序。
要求:父类Shape2D为一个抽象类,其中包含计算周长和计算面积两个方法。其他形状圆(Circle),三角形(Triangle)和长方形(Rectangle)均继承自Shape2D类。而Square继承自Rectangle类。编写测试类来计算这些形状的面积。
提示:不同的形状包含的属性应该不同,构造函数应该不同,计算周长和面积的方法也应该不同。
Shape.java
package shiyan23;
public abstract class shape2d {
public float PI = (float)
Math.PI;
public abstract double Perimeter();
public abstract double Acreage();
}
Circle.java
package shiyan23;
public class Circle extendsshape2d {
public double r ;
publicCircle(double r){
this.r=r;
}
public double Perimeter() {
return2*super.PI*r;
}
public double Acreage() {
return super.PI*r*r;
}
}
Triangle.java
package shiyan23;
public class Triangle extendsshape2d{
public double a,b,c;
publicTriangle(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
public double Perimeter() {
return a+b+c;
}
public double Acreage() {
double p =(a + b + c) /2;
double size = (double) Math.sqrt(p *(p - a) *(p - b) *(p - c));
return size;
}
}
Rectangle.java
package shiyan23;
public class Rectangle extendsshape2d{
public double l,k;
public Rectangle(double l,double k){
this.l=l;
this.k=k;
}
public double Perimeter() {
return (l+k)*2.0;
}
public double Acreage() {
return l*k;
}
}
Square.java
package shiyan23;
public class Square extendsRectangle{
publicSquare(double l) {
super(l, l);
}
}
ceshi_shape2d.java
package shiyan23;
import java.util.Scanner;
public class ceshi_shape2d {
public static void main(String[] args) {
System.out.println("下面构造一个圆形,请输入半径R:");
Scannerinput = new Scanner(System.in);
if (input.hasNextDouble()) {
Doublef = input.nextDouble();
Circlecircle = new Circle(f);
System.out.println("圆的周长:"+(double)Math.round(circle.Perimeter()*100)/100);//四舍五入保留2位小数
System.out.println("圆的面积:"+(double)Math.round(circle.Acreage()*100)/100);
}else {
// 输入错误的信息
System.out.println("输入有误!");
}
System.out.println("下面构造一个三角形,请输入三条边长(注意三边的关系):");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
while(panduansanbian(a,b,c)){
System.out.println("三条边不满足,请重新输入(注意三边的关系):");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
}
Triangletriangle = new Triangle(a, b, c);
System.out.println("三角形的周长:"+(double)Math.round(triangle.Perimeter()*100)/100);
System.out.println("三角形的面积:"+(double)Math.round(triangle.Acreage()*100)/100);
System.out.println("下面构造一个长方形,请输入长和宽:");
double l = input.nextDouble();
double k = input.nextDouble();
Rectanglerectangle = new Rectangle(l, k);
System.out.println("长方形的周长:"+(double)Math.round(rectangle.Perimeter()*100)/100);
System.out.println("长方形的面积:"+(double)Math.round(rectangle.Acreage()*100)/100);
System.out.println("下面构造一个正方形,请输入一个边长:");
double e = input.nextDouble();
Squaresquare = new Square(e);
System.out.println("正方形的周长:"+(double)Math.round(square.Perimeter()*100)/100);
System.out.println("正方形的面积:"+(double)Math.round(square.Acreage()*100)/100);
input.close();
}
public static boolean panduansanbian(double a,double b,double c){
if(a+b<=c||b+c<=a||b+c<=a){
return true;
}else{
return false;
}
}
}
(2.设计并实现一个直线(Line2D)类,可以通过三个构造函数分别使用点斜式、两点式和截距式三种方法来构造直线。并对直线做简单的比较。
要求:1. 点需要使用Point2D类表示,并且重写Point2D类的equals()方法,来判断两个点是否相同。
2. 重写Line2D类的equals()方法,判断两条直线是否同一条。
3. 编写boolean isParallel(Line2D)方法,判断两条直线是否平行。
提示:1. 使用两点式构造函数时,注意两点重合则无法唯一确定一条直线。
2. 使用点斜式时,当直线平行于y轴时,斜率为无穷大,但是Java语言中double类型可以表示无穷大,Double. NEGATIVE_INFINITY和Double. POSITIVE_INFINITY,请参阅Java API文档。
3. 建议将三种构造函数统一成一种表示方法,以方便比较直线是否相等。
4. 在判断两条直线是否平行时,首先要排除两条直线重合的情况。
Point2D.java
package shiyan3_2;
public class Point2D {
public double a,b;
publicPoint2D(double a,double b) {
this.a=a;
this.b=b;
}
public boolean equals(Point2D p){
if(a==p.a&&b==p.b)
return true;
return false;
}
}
Line2D.java
package shiyan3_2;
public class Line2D {
public double k,y0;
public Line2D(double k,
Point2D p){
this.k=k;
y0=p.b-p.a*k;
}
public Line2D(Point2D a,Point2Db){
k=(a.b-b.b)/(a.a-b.a);
y0=a.b-a.a*k;
if(a.a==b.a){
k=Double.POSITIVE_INFINITY;
y0=Double.NEGATIVE_INFINITY;
}else if(a.b==b.b){
k=Double.NEGATIVE_INFINITY;
y0=a.b;;
}
}
public Line2D(double x,double y){
k=-y/x;
y0=y;
}
public double getk(){
return k;
}
public double gety(){
return y0;
}
public boolean equals(Line2D p){
if(k==p.k&&y0==p.y0)
return true;
return false;
}
public boolean isParallel(Line2D p){
if(k==p.k&&y0!=p.y0)
return true;
return false;
}
}
ceshi_shape2d.java
package shiyan23;
import java.util.Scanner;
public class ceshi_shape2d {
public static void main(String[] args) {
System.out.println("下面构造一个圆形,请输入半径R:");
Scannerinput = new Scanner(System.in);
if (input.hasNextDouble()) {
Doublef = input.nextDouble();
Circlecircle = new Circle(f);
System.out.println("圆的周长:"+(double)Math.round(circle.Perimeter()*100)/100);//四舍五入保留2位小数
System.out.println("圆的面积:"+(double)Math.round(circle.Acreage()*100)/100);
}else {
// 输入错误的信息
System.out.println("输入有误!");
}
System.out.println("下面构造一个三角形,请输入三条边长(注意三边的关系):");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
while(panduansanbian(a,b,c)){
System.out.println("三条边不满足,请重新输入(注意三边的关系):");
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
}
Triangletriangle = new Triangle(a, b, c);
System.out.println("三角形的周长:"+(double)Math.round(triangle.Perimeter()*100)/100);
System.out.println("三角形的面积:"+(double)Math.round(triangle.Acreage()*100)/100);
System.out.println("下面构造一个长方形,请输入长和宽:");
double l = input.nextDouble();
double k = input.nextDouble();
Rectanglerectangle = new Rectangle(l, k);
System.out.println("长方形的周长:"+(double)Math.round(rectangle.Perimeter()*100)/100);
System.out.println("长方形的面积:"+(double)Math.round(rectangle.Acreage()*100)/100);
System.out.println("下面构造一个正方形,请输入一个边长:");
double e = input.nextDouble();
Squaresquare = new Square(e);
System.out.println("正方形的周长:"+(double)Math.round(square.Perimeter()*100)/100);
System.out.println("正方形的面积:"+(double)Math.round(square.Acreage()*100)/100);
input.close();
}
public static boolean panduansanbian(double a,double b,double c){
if(a+b<=c||b+c<=a||b+c<=a){
return true;
}else{
return false;
}
}
}
(2.设计并实现一个直线(Line2D)类,可以通过三个构造函数分别使用点斜式、两点式和截距式三种方法来构造直线。并对直线做简单的比较。
要求:1. 点需要使用Point2D类表示,并且重写Point2D类的equals()方法,来判断两个点是否相同。
2. 重写Line2D类的equals()方法,判断两条直线是否同一条。
3. 编写boolean isParallel(Line2D)方法,判断两条直线是否平行。
提示:1. 使用两点式构造函数时,注意两点重合则无法唯一确定一条直线。
2. 使用点斜式时,当直线平行于y轴时,斜率为无穷大,但是Java语言中double类型可以表示无穷大,Double. NEGATIVE_INFINITY和Double. POSITIVE_INFINITY,请参阅Java API文档。
3. 建议将三种构造函数统一成一种表示方法,以方便比较直线是否相等。
4. 在判断两条直线是否平行时,首先要排除两条直线重合的情况。
Point2D.java
package shiyan3_2;
public class Point2D {
public double a,b;
publicPoint2D(double a,double b) {
this.a=a;
this.b=b;
}
public boolean equals(Point2D p){
if(a==p.a&&b==p.b)
return true;
return false;
}
}
Line2D.java
package shiyan3_2;
public class Line2D {
public double k,y0;
public Line2D(double k,
Point2D p){
this.k=k;
y0=p.b-p.a*k;
}
public Line2D(Point2D a,Point2Db){
k=(a.b-b.b)/(a.a-b.a);
y0=a.b-a.a*k;
if(a.a==b.a){
k=Double.POSITIVE_INFINITY;
y0=Double.NEGATIVE_INFINITY;
}else if(a.b==b.b){
k=Double.NEGATIVE_INFINITY;
y0=a.b;;
}
}
public Line2D(double x,double y){
k=-y/x;
y0=y;
}
public double getk(){
return k;
}
public double gety(){
return y0;
}
public boolean equals(Line2D p){
if(k==p.k&&y0==p.y0)
return true;
return false;
}
public boolean isParallel(Line2D p){
if(k==p.k&&y0!=p.y0)
return true;
return false;
}
}
ceshi_Line2D.java
package shiyan3_2;
import java.util.Scanner;
public class ceshi_Line2D {
public static void main(String[] args) {
Scannerinput = new Scanner(System.in);
System.out.println("用点斜式构造直线,输入斜率和一个点的坐标");
double k,x,y;
k=input.nextDouble();
x=input.nextDouble();
y=input.nextDouble();
Point2Dd1 = new Point2D(x, y);
Line2Dl1 = new Line2D(k, d1);
System.out.println("构造的直线斜率为:"+l1.getk()+" 在y轴的截距为:"+l1.gety());
System.out.println("用俩点式构造直线,输入俩个点的坐标");
double x1,y1,x2,y2;
x1=input.nextDouble();
y1=input.nextDouble();
x2=input.nextDouble();
y2=input.nextDouble();
Point2Dd2 = new Point2D(x1, y1);
Point2Dd3 = new Point2D(x2, y2);
while(d2.equals(d3)){
System.out.println("俩点重合,请重新输入:");
x1=input.nextDouble();
y1=input.nextDouble();
x2=input.nextDouble();
y2=input.nextDouble();
d2.a=x1;
d2.b=y1;
d3.a=x2;
d3.b=y2;
}
Line2Dl2 = new Line2D(d2, d3);
System.out.println("构造的直线斜率为:"+l2.getk()+" 在y轴的截距为:"+l2.gety());
System.out.println("用截距式构造直线,输入俩个截距值:");
x=input.nextDouble();
y=input.nextDouble();
while(x==0||y==0){
System.out.println("截距不能为0,请重新输入:");
x=input.nextDouble();
y=input.nextDouble();
}
Line2Dl3 = new Line2D(x, y);
System.out.println("构造的直线斜率为:"+l3.getk()+" 在y轴的截距为:"+l3.gety());
if(l1.equals(l2)){
System.out.println("1和2两条直线重合");
}else{
System.out.println("1和2两条直线不重合");
}
if(l1.equals(l3)){
System.out.println("1和3两条直线重合");
}else{
System.out.println("1和3两条直线不重合");
}
if(l2.equals(l3)){
System.out.println("2和3两条直线重合");
}else{
System.out.println("2和3两条直线不重合");
}
if(l1.isParallel(l2)){
System.out.println("1和2俩条直线平行");
}else{
System.out.println("1和2俩条直线不平行");
}
if(l1.isParallel(l3)){
System.out.println("1和3俩条直线平行");
}else{
System.out.println("1和3俩条直线不平行");
}
if(l2.isParallel(l3)){
System.out.println("2和3俩条直线平行");
}else{
System.out.println("2和3俩条直线不平行");
}
input.close();
}
}
(3.设计并实现三个类:班级(Class)、学生(Student)、课程(Course)。然后计算班上每个同学的加权成绩。最后允许用户输入学生的学号查询学生信息。
要求:班级类的私有属性包含班级名称、学生人数以及学生列表(用对象数组表示)。班级类的方法包括:
1.构造函数Class(int nSutdent)
2.使用addStudent(Student student)来往班级中添加学生
3.使用calculateWeightedGrade()方法计算班上所有学生的加权成绩
4.]重写toString()方法来将班级信息转换成字符串
5.编写方法获得班上的学生
学生类的私有属性包含学生的学号、姓名、所选课程数量、所选课程(用对象数组表示)。学生类的方法包括
1.构造函数Student(int id, String name, intnCourse)
2.使用addCourse(Course course)来往学生类中添加课程
3.使用calculateWeightedGrade()来计算该学生的加权成绩
4.重写toString()方法来将学生信息转换成字符串
5.使用printStudentInfo()方法来打印学生的基本信息
课程类包含私有属性课程编号、课程名称、学分、是否缓考以及成绩(成绩要求在0到100分之间)。方法包括获取课程各个属性的方法。
构造函数Course(int id, String name, intcredit)
使用方法设置是否缓考和成绩
重写toString()方法来将课程信息转换成字符串
编写一个测试类来测试这三个类的使用。要求最少往班级中创建5个学生,学生可以选修0到最多5门课程。程序运行后,先计算好班级中每个同学的加权成绩。然后等待用户输入学号查询学生,如果有对应的学生,则输出学生的信息、选修课程信息、最后输出加权成绩。直到用户输入-1退出程序。
提示:加权成绩的计算公式为:
其中,g表示最后的加权成绩,n表示同学选修的课程数,si表示第i门课的成绩,wi表示第i门课的学分。
注意:如果某生某课程缓考,则该门课成绩不能计入加权成绩内。此外,类中不可出现公用属性,必须使用封装。
Class.java
package shiyan3_3;
public class Class {
privateString classname;
private int studentnumber;
privateStudent[] account = newStudent[10];
publicClass(String name) {
classname=name;
studentnumber=0;
}
publicString getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public int getStudentnumber() {
return studentnumber;
}
public void setStudentnumber(int studentnumber) {
this.studentnumber = studentnumber;
}
public void addStudent(Student student){
account[studentnumber]=student;
studentnumber++;
}
public double calculateWeightedGrade(){
double sum=0;
for(int i=0;i<studentnumber;i++){
sum+=account[i].calculateWeightedGrade();
}
return sum/studentnumber;
}
publicString toString() {
return "Class [班级名称=" + classname + ",学生人数=" + studentnumber + ",总学生加权成绩="
+calculateWeightedGrade() +"]";
}
public int findstudent(int num){
for(int i=0;i<studentnumber;i++){
if(account[i].getxuehao()==num)
return i+1;
}
return 0;
}
public void printstudentinfo(int num){
System.out.println(account[num-1].toString());
account[num-1].printcourse();
}
}
Student.java
package shiyan3_3;
public class Student {
private int xuehao;
privateString name;
private int classnumber=0;
privateCourse[] courses = new Course[10];//对象数组需要初始化
public int getclassnumber(){
return classnumber;
}
publicString getname(){
return name;
}
public int getxuehao(){
return xuehao;
}
publicStudent(int id,String name){
xuehao=id;
this.name=name;
}
public void addCourse(Course course){
courses[classnumber]=course;
classnumber++;
}
public double calculateWeightedGrade(){
if(classnumber==0)
return 0;
double sum=0;
double u=0;
for(int i=0;i<classnumber;i++){
if(courses[i].isIsflag()){
continue;
}else{
sum+=courses[i].getXuefen()*courses[i].getXuefen();
u+=courses[i].getXuefen();
}
}
return sum/u;
}
publicString toString() {
return "Student [学号=" + xuehao + ",姓名=" + name + ",课程数=" + classnumber + ",加权成绩=" + calculateWeightedGrade() + "]";
}
public void printcourse(){
for(int i=0;i<classnumber;i++){
System.out.println(courses[i].toString());
}
}
}
Course.java
package shiyan3_3;
public class Course {
private int idnumber;
private String coursename;
private double xuefen;
private boolean isflag=false;
private double chengji;
public Course(int idnumber, String coursename, double xuefen, boolean isflag, double chengji) {
this.idnumber = idnumber;
this.coursename = coursename;
this.xuefen = xuefen;
this.isflag = isflag;
this.chengji = chengji;
}
public String toString() {
return "Course [课程编号=" + idnumber + ",课程名=" + coursename + ",课程学分=" + xuefen + ",是否缓考=" + isflag
+", 成绩=" + chengji + "]";
}
public int getIdnumber() {
return idnumber;
}
public void setIdnumber(int idnumber) {
this.idnumber = idnumber;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
public double getXuefen() {
return xuefen;
}
public void setXuefen(double xuefen) {
this.xuefen = xuefen;
}
public boolean isIsflag() {
return isflag;
}
public void setIsflag(boolean isflag) {
this.isflag = isflag;
}
public double getChengji() {
return chengji;
}
public void setChengji(double chengji) {
this.chengji = chengji;
}
}
Ceshi.java
package shiyan3_3;
import java.util.Scanner;
public class ceshi {
public static void main(String[] args) {
Coursec1 = new Course(1, "语文", 5, false, 90);
Coursec2 = new Course(2, "数学", 6, false, 99);
Coursec3 = new Course(3, "英语", 4, false, 80);
Coursec4 = new Course(4, "物理", 5, false, 95);
Coursec5 = new Course(5, "化学", 5, false, 90);
Coursec6 = new Course(6, "生物", 6, false, 89);
Coursec7 = new Course(7, "政治", 4, true, 70);
Coursec8 = new Course(8, "地理", 5, false, 95);
Studentstu1 =newStudent(2016110, "小红");
Studentstu2 =newStudent(2016111, "小橙");
Studentstu3 =newStudent(2016112, "小黄");
Studentstu4 =newStudent(2016113, "小绿");
Studentstu5 =newStudent(2016114, "小蓝");
Classclass1 =new Class("1班");
stu1.addCourse(c1);
stu1.addCourse(c2);
stu1.addCourse(c3);
stu2.addCourse(c4);
stu2.addCourse(c5);
stu3.addCourse(c1);
stu3.addCourse(c3);
stu3.addCourse(c5);
stu3.addCourse(c7);
stu4.addCourse(c8);
stu5.addCourse(c4);
stu5.addCourse(c5);
stu5.addCourse(c6);
stu5.addCourse(c7);
class1.addStudent(stu1);
class1.addStudent(stu2);
class1.addStudent(stu3);
class1.addStudent(stu4);
class1.addStudent(stu5);
System.out.println(stu1.toString());
System.out.println(stu2.toString());
System.out.println(stu3.toString());
System.out.println(stu4.toString());
System.out.println(stu5.toString());
System.out.println(class1.toString());
System.out.println("请输入查询学生的学号:");
Scannerinput = new Scanner(System.in);
int ant = input.nextInt();
while(ant!=-1){
if(class1.findstudent(ant)!=0){
class1.printstudentinfo(class1.findstudent(ant));
System.out.println("已找到,请再输入查询学生的学号:");
ant = input.nextInt();
}else{
System.out.println("未找到,请再输入查询学生的学号:");
ant = input.nextInt();
}
}
input.close();
System.exit(1);
}
}