2024秋pta面向对象程序设计练习二

6-1:

class Circle{

double radius;

public Circle(double r){

this.radius=r;

}

public double getArea()

{return Math.PI*radius*radius;}

public double getLength(){

return 2*Math.PI*radius;

}}

6-2:

 

abstract class Vehicle{

public abstract double rent();

}

class Truck extends Vehicle{

private double load;

public Truck(double load){

this.load=load;

}

public double rent(){

return load*1000;

}

}

class Keche extends Vehicle{

private int seats;

public Keche(int seats){

this.seats=seats;

}

public double rent(){

return seats*50.0;

}

}

class Car extends Vehicle{

private int level;

private int year;

public Car(int a,int b){

level=a;

year=b;

}

public double rent(){

return 200.0*level/Math.sqrt((double)year);

}

}

class CarRentCompany{

static double total;

public static double rentVehicles(Vehicle[] vs){

for(Vehicle c:vs){

total+=c.rent();

}

return total;

}

}

 

 6-3:

abstract class HiINC{

public static int day;

public static double f;

public abstract double getRent();

public static double calculateRent(HiINC[] hi, int days) {

day=days;

for(HiINC c:hi){

f+=c.getRent();

}

return f;

}

}

class Bicycle extends HiINC{

String name;

public Bicycle(String a){

name=a;

}

public double getRent(){

return day*12.0;

}

}

class Car extends HiINC{

String name;

double price;

public Car(String a,double b){

name=a;

price=b;

}

public double getRent(){

return day*price*0.001;

}

}

 

6-4:

 

class Undergraduate extends Student{

public Undergraduate(String id,double ca){

super(id,ca);

}

public boolean isReq(){

if(getCA()>=80){

return true;

}

else return false;

}

}

class Graduate extends Student{

public boolean supervisor;

public Graduate(String id,double ca,boolean supervisor){

super(id,ca);

this.supervisor = supervisor;

}

public boolean isReq(){

return (getCA()>=85&&supervisor==true);

}

}

7-1:

 

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int[] grade = new int[5];

for(int i=0; i<grade.length; i++){

grade[i] = in.nextInt();

}

RR rr = new RT(grade);

double dd = rr.mark();

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

}

}

abstract class RR{

int[] grade;

public RR(int[] grade){

this.grade = grade;

}

public abstract double mark();

}

class RT extends RR{

public RT(int[] grade){

super(grade);

}

public double mark(){

for(int i=0;i<grade.length-1;i++){

for(int j=0;j<grade.length-1-i;j++){

if(grade[j]>grade[j+1]){

int tem=grade[j];

grade[j]=grade[j+1];

grade[j+1]=tem;

}

}

}

return (grade[1]+grade[2]+grade[3])/3.0;}

}

 

7-2:

 

public class Main {

public static void main(String[] args) {

Animal animal = new Dog();

animal.shout();

((Dog)animal).run();

}

}

class Animal {

void shout() {

System.out.println("animal shout!");

}

}

class Dog extends Animal {

void shout() {

super.shout();

System.out.println("wangwang……");

}

void run() {

System.out.println("Dog is running");

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值