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");
}
}
原文地址:https://blog.csdn.net/2303_79998710/article/details/142410690
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!