您的位置:首页 > 编程语言 > Java开发

3.28 java 上机实验 继承

2018-03-28 11:21 429 查看
【shape】

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());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  继承