美文网首页
Java第三次作业

Java第三次作业

作者: z坎坷 | 来源:发表于2018-11-08 15:47 被阅读0次

131 员工类

import java.util.*;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan =new Scanner(System.in);

int ms=scan.nextInt();

int ss=scan.nextInt();

int ticheng=scan.nextInt();

int days=scan.nextInt();

int daysalary=scan.nextInt();

employee p1=new manager(ms);

employee p2=new salesman(ss,ticheng);

employee p3=new worker(days,daysalary);

System.out.println(p1.getSalary());

System.out.println(p2.getSalary());

System.out.println(p3.getSalary());

}

}

class employee{

int salary;

public employee() {

salary=0;

}

public employee(int s) {

salary=s;

}

int getSalary() {

return salary;

}

}

class manager extends employee{

public manager(int s) {

super(s);

}

int getSalary() {

return salary;

}

}

class salesman extends employee{

int ticheng;

public salesman(int s,int t){

super(s);

ticheng=t;

}

int getSalary() {

return salary+ticheng;

}

}

class worker extends employee{

int days;

int daysalary;

public worker(int d,int ds) {

days=d;

daysalary=ds;

}

int getSalary() {

return days*daysalary;

}

}

####################################

132 员工类-2

import java.util.*;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan =new Scanner(System.in);

int ms=scan.nextInt();

int ss=scan.nextInt();

int ticheng=scan.nextInt();

int days=scan.nextInt();

int daysalary=scan.nextInt();

employee p1=new manager(ms);

employee p2=new salesman(ss,ticheng);

employee p3=new worker(days,daysalary);

System.out.println(p1.getSalary());

System.out.println(p2.getSalary());

System.out.println(p3.getSalary());

}

}

abstract class employee{

int salary;

public employee() {

salary=0;

}

public employee(int s) {

salary=s;

}

abstract int getSalary();

}

class manager extends employee{

public manager(int s) {

super(s);

}

int getSalary() {

return salary;

}

}

class salesman extends employee{

int ticheng;

public salesman(int s,int t){

super(s);

ticheng=t;

}

int getSalary() {

return salary+ticheng;

}

}

class worker extends employee{

int days;

int daysalary;

public worker(int d,int ds) {

days=d;

daysalary=ds;

}

int getSalary() {

return days*daysalary;

}

}

############################################

133-Shape类-2

import java.util.*;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan =new Scanner(System.in);

double a=scan.nextInt();

double x=scan.nextInt();

double y=scan.nextInt();

double r=scan.nextInt();

shape p1=new Square(a);

shape p2=new Rectangle(x,y);

shape p3=new Circle(r);

System.out.printf("%.2f ",p1.getPerimeter());

        System.out.printf("%.2f\n",p1.getArea());

        System.out.printf("%.2f ",p2.getPerimeter());

        System.out.printf("%.2f\n",p2.getArea());

        System.out.printf("%.2f ",p3.getPerimeter());

        System.out.printf("%.2f\n",p3.getArea());

}

}

class shape{

double getPerimeter() {

return 0;

}

double getArea() {

return 0;

}

}

class Square extends shape{

double x;

public Square(double xx) {

x=xx;

}

double getPerimeter() {

return 4*x;

}

double getArea() {

return x*x;

}

}

class Rectangle extends Square{

double y;

public Rectangle(double xx,double yy){

super(xx);

y=yy;

}

double getPerimeter() {

return 2*(x+y);

}

double getArea() {

return x*y;

}

}

class Circle extends shape{

double r;

public Circle(double rr) {

r=rr;

}

double getPerimeter() {

return 2*Math.PI*r;

}

double getArea() {

return Math.PI*r*r;

}

}

####################################

134 Shape类-3

import java.util.*;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scan =new Scanner(System.in);

double a=scan.nextDouble;

double x=scan.nextDouble;

double y=scan.nextDouble;

double r=scan.nextDouble;

shape p1=new Square(a);

shape p2=new Rectangle(x,y);

shape p3=new Circle(r);

System.out.printf("%.2f ",p1.getPerimeter());

        System.out.printf("%.2f\n",p1.getArea());

        System.out.printf("%.2f ",p2.getPerimeter());

        System.out.printf("%.2f\n",p2.getArea());

        System.out.printf("%.2f ",p3.getPerimeter());

        System.out.printf("%.2f\n",p3.getArea());

}

}

abstract class shape{

abstract double getPerimeter();

abstract double getArea() ;

}

class Square extends shape{

double x;

public Square(double xx) {

x=xx;

}

double getPerimeter() {

return 4*x;

}

double getArea() {

return x*x;

}

}

class Rectangle extends Square{

double y;

public Rectangle(double xx,double yy){

super(xx);

y=yy;

}

double getPerimeter() {

return 2*(x+y);

}

double getArea() {

return x*y;

}

}

class Circle extends shape{

double r;

public Circle(double rr) {

r=rr;

}

double getPerimeter() {

return 2*Math.PI*r;

}

double getArea() {

return Math.PI*r*r;

}

}

#######################################

137 学生、大学生、研究生类-2

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int no = scan.nextInt();

        String name = scan.next();

        String sex = scan.next();

        String major = scan.next();

        int score = scan.nextInt();

        Student s1,s2;

        s1 = new CollegeStudent(no, name, sex, major,score);

        no = scan.nextInt();

        name = scan.next();

        sex = scan.next();

        major = scan.next();

        String supervisor = scan.next();

        score = scan.nextInt();

        s2 = new GraduateStudent(no, name, sex, major, supervisor, score);

        s1.getGrade();

        s2.getGrade();

        scan.close();

    }

}

abstract class Student {

    int no;

    String name;

    String sex;

    int score;

    Student(int n,String na,String s,int sc){

    no = n;

    name = na;   

        sex = s;

        score = sc;

    }

    public void setSex(String s) {

        sex = s;

    }

    public void setName(String na) {

        name = na;

    }

    public void setNo(int n) {

        no = n;

    }

    public int getNo() {

        return no;

    }

    public String getName() {

        return name;

    }

    public String getSex() {

        return sex;

    }

    public int getScore() {

        return score;

    }

    public void setScore(int sc) {

        score = sc;

    }

    public void print(){

        System.out.println("no: "+getNo());

        System.out.println("name: "+getName());

        System.out.println("sex: "+getSex());

    }

    public abstract void getGrade();

}

class CollegeStudent extends Student {

    String major;

    CollegeStudent(int no, String name, String sex, String m, int score){

        super(no,name,sex,score);

        major = m;

    }

    public String getMajor() {

        return major;

    }

    public void setMajor(String m) {

        major = m;

    }

    public void print(){

        super.print();

        System.out.println("major: "+major);

    }

    public void getGrade(){

        if(score>=80){

            System.out.println("A");

        }else if(score>=70){

            System.out.println("B");

        }else if(score>=60){

            System.out.println("C");

        }else if(score>=50){

            System.out.println("D");

        }else{

            System.out.println("E");

        }

    }

}

class GraduateStudent extends CollegeStudent {

    String supervisor;

    GraduateStudent(int no, String name, String sex, String major, String su, int score) {

        super(no, name, sex, major, score);

        supervisor = su;

    }

    public String getSupervisor() {

        return supervisor;

    }

    public void setSupervisor(String su) {

        supervisor = su;

    }

    public void print() {

        super.print();

        System.out.println("supervisor: " + supervisor);

    }

    public void doResearch() {

        System.out.println(getName() + " is doing research");

    }

    public void getGrade() {

        if (score >= 90) {

            System.out.println("A");

        } else if (score >= 80) {

            System.out.println("B");

        } else if (score >= 70) {

            System.out.println("C");

        } else if (score >= 60) {

            System.out.println("D");

        } else {

            System.out.println("E");

        }

    }

}

#######################################

138打球过程

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {

        BallMatch p;

        Scanner scan = new Scanner(System.in);

      int type = scan.nextInt();

        String score = scan.next();

        if(type==1) {

        p= new FootballMatch(score);

                p.compete(score);

        }

        if(type==2) {

        p = new BasketBallMatch(score);

            p.compete(score);

        }

        }

    }

abstract class BallMatch{

    public final void compete(String score){

        checkin();

        start();

        play();

        end();

        annouceResult(score);

    }

    public void checkin(){

        System.out.println("now checking in");

    }

    public void start(){

        System.out.println("now starting");

    }

    public abstract void play();

    public void end(){

        System.out.println("now ending");

    }

    public void annouceResult(String result){

        System.out.println("now annoucing result: "+result);

    }

}

class FootballMatch extends BallMatch{

    String score;

    FootballMatch(String s){

        score = s;

    }

    public void play(){

        System.out.println("now playing football");

    }

}

class BasketBallMatch extends BallMatch{

    String score;

    BasketBallMatch(String s){

        score=s;

    }

    public void play(){

        System.out.println("now playing basketball");

    }

}

相关文章

网友评论

      本文标题:Java第三次作业

      本文链接:https://www.haomeiwen.com/subject/znuwxqtx.html