3.28 java 上机实验 继承
2018-03-28 11:21
429 查看
【shape】
【fraction】
【student】
package se; import java.util.Scanner; import java.util.*; public class sdf { public static class Shape extends Object { protected double area; protected int c; public Shape(double s,int c2) { area=s; c=c2; } public double getarea() { return area; } public int getcc() { return c; } } public static class TwoDimensional extends Shape { protected int x; protected int y; public TwoDimensional(double s,int cc,int x,int y) { super(s,cc); this.x=x; this.y=y; } public int getx() { return x; } public int gety() { return y; } } public static class ThreeDimensional extends Shape { protected int x,y,z; public ThreeDimensional(double s,int cc,int x,int y,int z) { super(s,cc); this.x=x; this.y=y; this.z=z; } public int getx() { return x; } public int gety() { return y; } public int getz() { return z; } } public static class rect extends TwoDimensional { private int a,b; public rect(int a,int b) { super(0,0,0,0); this.a=a; this.b=b; } @Override public double getarea() { return area=a*b; } @Override public int getcc() { return c=2*(a+b); } } public static class tri extends TwoDimensional { public int h,d; public int a,b,e; public tri(int h,int d,int a,int b,int e) { super(0,0,0,0); this.h=h; this.d=d; this.a=a; this.b=b; this.e=e; } @Override public double getarea() { return area=h*d/2; } @Override public int getcc() { return c=a+b+e; } public void setx(int x) { this.x=x; } public void sety(int y) { this.y=y; } } public static class circle extends TwoDimensional { public double r; public double ci; public circle(double r) { super(0,0,0,0); this.r = r; } @Override public double getarea() { return area=r*r*3.14; } public double getc() { return ci=r*2*3.14; } } public static class cube extends ThreeDimensional { private int a,b,h; public cube(int a,int b,int h) { super(0,0,0,0,0); this.a=a; this.b=b; this.h=h; } @Override public double getarea() { return area=a*b*2+a*h*2+b*h*2; } public double getc2() { return c=4*(a+b+h); } public void setx(int x) { this.x=x; } public void sety(int y) { this.y=y; } public void setz(int z) { this.z=z; } } public static void main(String arg[]) { tri s= new tri(3,4,3,4,5); double area=s.getarea(); int c=s.getcc(); System.out.println("Triangle "); System.out.println("the Area is "+area); System.out.println("the circumference is "+c); System.out.println("the position of tri is (x,y) = ("+s.x+","+s.y+")"); s.setx(10); s.sety(20); System.out.println("the position of tri is (x,y) = ("+s.x+","+s.y+")"); System.out.println("Rectangle "); rect r=new rect(10,15); double rarea=r.getarea(); int rc=r.getcc(); System.out.println("the Area is "+rarea); System.out.println("the circumference is "+rc); System.out.println("Cube "); cube t=new cube(3,5,7); System.out.println("the Area is "+t.getarea()); System.out.println("the circumference is "+t.getc2()); } }
【fraction】
package se; import java.util.Scanner; public class fraction { public static class frac{ private int a,b; public frac() { int a=1; int b=1; } public frac(int a,int b) { this.a=a; this.b=b; if(this.b==0) { System.out.println("error! the denominator can not be 0!\n"); } } public void add(int c,int d) { this.a=a+c; this.b=b+d; } public void sub(int c,int d) { this.a=a-c; this.b=b-d; if(this.b==0) { System.out.println("error! the denominator can not be 0!\n"); } } public void mul(int c,int d) { this.a=a*c; this.b=b*d; if(this.b==0) { System.out.println("error! the denominator can not be 0!\n"); } } public void div(int c,int d) { if(d==0) { System.out.println("error! the denominator can not be 0!\n"); return; } this.a=a/c; this.b=b/d; if(this.b==0) { System.out.println("error! the denominator can not be 0!\n"); } } public int gcd(int a,int b) { if(b==0)return a; else return gcd(b,a%b); } public void pr() { int d=gcd(a,b); System.out.printf("%d/%d\n",a/d,b/d); } public void pr2() { int d=gcd(a,b); double kk=(a/d*1.0) / (b/d*1.0); System.out.println(kk); } } public static void main(String[] args) { // TODO Auto-generated method stub frac a = new frac(45,15); System.out.println("output orignal fraction in the form a/b"); a.pr(); System.out.println("output in floating-point format"); a.pr2(); System.out.println(" Add two Rational numbers"); a.add(4, 3); System.out.println("output fraction in the form a/b"); a.pr(); System.out.println("output in floating-point format"); a.pr2(); a.sub(4, 3); System.out.println("output fraction in the form a/b"); a.pr(); System.out.println("output in floating-point format"); a.pr2(); a.mul(2, 10); System.out.println("output fraction in the form a/b"); a.pr(); System.out.println("output in floating-point format"); a.pr2(); a.div(3, 5); System.out.println("output fraction in the form a/b"); a.pr(); System.out.println("output in floating-point format"); a.pr2(); frac b=new frac(); b.add(1, 3); System.out.println("output fraction in the form a/b"); b.pr(); System.out.println("output in floating-point format"); b.pr2(); } }
【student】
package se; public class student { public static class Student { protected String name; protected int age; public Student(String s,int age) { name=s; this.age=age; } public String getname() { return name; } public int getage() { return age; } } public static class GraduatedStudent extends Student { private int graduateyear; private String Organization; public GraduatedStudent(String n,int a,int y,String s) { super(n,a); graduateyear=y; Organization = s; } public int getyear() { return graduateyear; } public String getorg() { return Organization; } public String toString() { return String.format("%s:%s %s:%d %s:%s %s:%d","name",super.name,"age",super.age,"Organization",Organization,"graduateyear",graduateyear); } } public static class underGraduateStudent extends Student { private int Grade; private String major; public underGraduateStudent(String n,int a,int y,String s) { super(n,a); Grade=y; major = s; } public int getGrade() { return Grade; } public String getmajor() { return major; } public String toString() { return String.format("%s:%s %s:%d %s:%s %s:%d","name",super.name,"age",super.age,"major",major,"Grade",Grade); } } public static void main(String[] args) { GraduatedStudent a=new GraduatedStudent("cc",28,2017,"中国银行"); System.out.printf(a.toString()); System.out.println(); underGraduateStudent b =new underGraduateStudent("kk",20,3,"计算机科学与技术"); System.out.printf(b.toString()); } }
相关文章推荐
- java 上机实验五 子类与继承
- 实验3_Java类的继承和接口的定义和使用
- JAVA面向对象程序设计,董小园 清华大学出版社 课后上机实验题代码 第2章
- 第八周JAVA实验之继承
- 第三周JAVA上机实验(一)进行分数的加法
- 第二周JAVA上机实验(三)猜数游戏
- java实验三 类的继承与多态
- 《通过实验看 Java 中私有成员是否能被继承》续
- 《java第十周实验报告2-1》---人,员工继承练习
- 简单的java实验,涉及到 类继承以及接口问题,方法体的重写(区别于重载)
- Java第4次实验提纲(面向对象2-继承、多态、抽象类与接口与Swing)
- java课上机实验
- 第二周JAVA上机实验(一)输出26个希腊字母
- java上机实验
- 软件体系结构上机实验 面向对象体系结构风格的 KWIC 关键词索引系统设计与实现 java
- C++ 上机实验(二)【继承与多态】
- 第二周JAVA上机实验(二)判断这个数是几位数、是否是回文数
- 通过实验看 Java 中私有成员是否能被继承
- Java上机实验之接口
- Java 继承与多态实验