以下为《java复习资料》的无排版文字预览,完整内容请下载
package day1;
import java.util.*;
class Circle{
private double radius;
Circle(double radius){
this.radius=radius;
}
Circle(){
radius=0;
}///构造函数一般情况下都要写
double getRadius() {
return radius;
}
void setRadius(double radius) {
this.radius=radius;
}
double getArea() {
return Math.PI*radius*radius;
}
double getPerimeter() {
return Math.PI*2*radius;
}////get、set后面用到该类时某某
public String toString() {
String a=String.format("%.2f",radius);////特殊函数重点
return "Circle(r:"+a+")";
}
}
class Cone{
private double height;
private Circle circle;
Cone(double height,Circle circle){
this.height=height;
this.circle=circle;///类也是一种类型,按普通类型处理
}
Cone(){
height=0;
circle.setRadius(0);///因为权限问题没法使用时,注意到对象就是该类,即可使用权限,直接通过方法初始化
}
double getVolume() {
return 1/3*height*circle.getArea();
}
public String toString() {
String m=String.format("%.2f",height);
String n=String.format("%.2f", circle.getRadius());
return "Circle(h:"+m+",Circle(r:"+n+"))";
}
}
//在这里给出函数被调用进行测试的例子。例如:
//import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 0; i < n; i++) {
String str = input.next();
if(str.equals("Circle")) {
Circle c = new Circle(input.nextDouble());
System.out.println("The area of " + c.toString() + " is " + String.format("%.2f",c.getArea()));
System.out.println("The perimeter of " + c.toString() + " is "+ String.format("%.2f",c.getPerimeter()));
} else if(str.equals("Cone")) {
Cone r = new Cone(input.nextDouble(), new Circle(input.nextDouble()));
System.out.println("The volume of " + r.toString() + " is " + String.format("%.2f",r.getVolume()));
}
}
}
}
package day1;
import java.util.*;
abstract class Animal{
private String name; //名字
private int age; //年龄
public abstract void info(); //返回动物信息
public abstract void speak(); //动物叫
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Dog extends Animal{
int violence;
public Dog(String name,int age,int violence) {
super(name,age);///可以直接使用父类的
this.violence=violence;
}
public void info() {
System.out.println("该狗的名字是"+getName()+",年龄是"+getAge()+"岁,凶猛程度是"+violence+"度");
}
public void speak() {
System.out.println("旺旺");
}
}
class Cat extends Animal{
int mousingAbility;
Cat(String name,int age,int mousingAbility){
super(name,age);
this.mousingAbility=mousingAbility;
}
public void info() {
System.out.println("该猫的名字是"+getName()+",年龄是"+getAge()+"岁,捕鼠能力是"+mousingAbility+"分");
}
public void speak() {
System.out.println("喵喵");
}
}
class Factory {
Animal getAnimalByType(int type,String name,int age,int ownAttribute) {
if(type==1)
{
Animal animal=new Dog(name,age,ownAttribute);///此时使用构造方法直接传参,不用再使用类区新建一个对象再进行属性的声明。
return animal;
}
else
{
Animal animal=new Cat(name,age,ownAttribute);
return animal;
}
}
}
public class Main {
public static void main(String[] args) {
Factory factory=new Factory();
Scanner input=new Scanner(System.in);
int type=input.nextInt();
String name=input.next();
int age=input.nextInt();
int ownAttribute=input.nextInt();
Animal a=factory.getAnimalByType(type, name, age, ownAttribute);
a.info();
a.speak();
System.out.println(a.getName());
System.out.println(a.getAge());
input.close();
}
}
/*请在这里填写你编写的Dog类、Cat类和Factory类 */
package day1;
import java.util.Scanner;
class Drink{
private int amount; //数量
private double price; //单价
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
Drink(int amount,double price){
this.amount=amount;
this.price=price;
}
public double getFee() {
return 0.0;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int amount1=sc.nextInt();//奶茶数量
double price1=sc.nextFloat();//奶茶单价
int amount2=sc.nextInt();//咖啡数量
double price2=sc.nextFloat();//咖啡单价
double sum=0;
for(int i=0;i请点击下方选择您需要的文档下载。
以上为《java复习资料》的无排版文字预览,完整内容请下载
java复习资料由用户“sdzj7588039”分享发布,转载请注明出处