BLOG-2
Java作为一种面向对象语言。支持以下基本概念:多态 继承 封装 抽象类 对象 实例 方法 重载
- 对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。
- 类:类是一个模板,它描述一类对象的行为和状态。
- public class Dog { String breed; int size; String colour; int age; void eat() { } void run() { } void sleep(){ } void name(){ } }局部变量:在方法、构造方法或者语句块中定义的变量被称为局部变量。变量声明和初始化都是在方法中,方法结束后,变量就会自动销毁。成员变量:成员变量是定义在类中,方法体之外的变量。这种变量在创建对象的时候实例化。成员变量可以被类中方法、构造方法和特定类的语句块访问。类变量:类变量也声明在类中,方法体之外,但必须声明为 static 类型。
-
一个类可以拥有多个方法,在上面的例子中:eat()、run()、sleep() 和 name() 都是 Dog 类的方法。
- 一个源文件中只能有一个 public 类
- 一个源文件可以有多个非 public 类
- 源文件的名称应该和 public 类的类名保持一致。例如:源文件中 public 类的类名是 Employee,那么源文件应该命名为Employee.java。
- 如果一个类定义在某个包中,那么 package 语句应该在源文件的首行。
- 如果源文件包含 import 语句,那么应该放在 package 语句和类定义之间。如果没有 package 语句,那么 import 语句应该在源文件中最前面。
- import 语句和 package 语句对源文件中定义的所有类都有效。在同一源文件中,不能给不同的类不同的包声明。
类有若干种访问级别,并且类也分不同的类型:抽象类和 final 类等。这些将在访问控制章节介绍。
除了上面提到的几种类型,Java 还有一些特殊的类。
在 Java 中,如果给出一个完整的限定名,包括包名、类名,那么 Java 编译器就可以很容易地定位到源代码或者类。import 语句就是用来提供一个合理的路径,使得编译器可以找到某个类。
- 题量;正常 难道;难。
- pta4
-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("wrong format");
}
} -
![]()
也是拿下15分好吧,终究是尽力了。
- pta5
-
import java.util.Calendar;
import java.util.Objects;
import java.util.Scanner;import static java.lang.System.exit;
//特色菜
class dish {
//特色菜的属性:菜名、口味、价格
String name;
String taste;
int price;public dish(String name, String taste, int price) {
this.name = name;
this.taste = taste;
this.price = price;
}public dish(String name, int price) {
this.name = name;
this.price = price;
}public String getName() {
return name;
}public String getTaste() {
return taste;
}public int getPrice() {
return price;
}public void setName(String name) {
this.name = name;
}public void setTaste(String taste) {
this.taste = taste;
}public void setPrice(int price) {
this.price = price;
}
}//普通菜
//桌号
//table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
class Table {
int num;
String name;
String phone;
String date;
//时间
String time;public Table(int num, String name, String phone, String date, String time) {
this.num = num;
this.name = name;
this.phone = phone;
this.date = date;
this.time = time;
}public int getNum() {
return num;
}public String getName() {
return name;
}public String getPhone() {
return phone;
}public String getDate() {
return date;
}public String getTime() {
return time;
}public void setNum(int num) {
this.num = num;
}public void setName(String name) {
this.name = name;
}public void setPhone(String phone) {
this.phone = phone;
}public void setDate(String date) {
this.date = date;
}public void setTime(String time) {
this.time = time;
}
}class User {
int num;
String name;
String phone;
int price;public User(int num, String name, String phone, int price) {
this.num = num;
this.name = name;
this.phone = phone;
this.price = price;
}
public String getName() {
return name;
}public String getPhone() {
return phone;
}public int getPrice() {
return price;
}public int getNum() {
return num;
}public void setName(String name) {
this.name = name;
}public void setPhone(String phone) {
this.phone = phone;
}public void setPrice(int price) {
this.price = price;
}public void setNum(int num) {
this.num = num;
}}
//点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 特色菜
class recorde {
int tableNum1;
int num;
String name;
String taste;
int portion;
int count;
public recorde(int num, String name, String taste, int portion, int count) {
this.num = num;
this.name = name;
this.taste = taste;
this.portion = portion;
this.count = count;
}//普通点菜
//序号、菜名、份额、份数
public recorde(int num, String name, int portion, int count) {
this.num = num;
this.name = name;
this.portion = portion;
this.count = count;
}public recorde(int tableNum1, int num, String name, String taste, int portion, int count) {
this.tableNum1 = tableNum1;
this.num = num;
this.name = name;
this.taste = taste;
this.portion = portion;
this.count = count;
}
public int getTableNum1() {
return tableNum1;
}public int getNum() {
return num;
}
public String getName() {
return name;
}public String getTaste() {
return taste;
}public int getPortion() {
return portion;
}public int getCount() {
return count;
}public void setNum(int num) {
this.num = num;
}public void setName(String name) {
this.name = name;
}public void setTaste(String taste) {
this.taste = taste;
}public void setPortion(int portion) {
this.portion = portion;
}public void setCount(int count) {
this.count = count;
}public void setTableNum1(int tableNum1) {
this.tableNum1 = tableNum1;
}}
public class Main {
//特色菜记录数
public static int dishNum = 0;
public static dish dish[] = new dish[100];
//普通菜记录数
//桌号
public static int tableNum = 0;
public static Table table[] = new Table[100];
public static recorde recorde[][] = new recorde[100][100];
public static int Sum[] = new int[100];public static int userNum = 0;
public static User user[] = new User[100];public static boolean flag1=false;
public static boolean flag2=false;
public static boolean flag3=false;public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
String str = scanner.nextLine();
if (str.equals("end"))
break;
//菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"
//
//例如:麻婆豆腐 川菜 9 T
//普通菜的输入格式:菜品名+英文空格+基础价格
//如果格式不对,直接输出wrong formatString[] str1 = str.split(" ");
//多条记录
//重复记录
if (str1.length == 4 && str1[3].equals("T")) {
dishNum++;
String name = str1[0];
String taste = str1[1];
int price = Integer.parseInt(str1[2]);
dish[dishNum] = new dish(name, taste, price);
int num=find4(name);
if(num!=-1) dish[num] = new dish(name, taste, price);
} else if (str1.length == 2) {
String name=str1[0];
int price=Integer.parseInt(str1[1]);
dish[++dishNum] = new dish(str1[0], Integer.parseInt(str1[1]));
int num=find4(name);
if(num!=-1) dish[num] = new dish(name, price);
}
else if(str1[2].equals("T")||str1[1].equals("T")||str1[0].equals("T"))
{
flag1=true;
}
//输入格式:
//table 1 : tom 13605054400 2023/5/1 20/30/00
else if (str1[0].equals("table"))//如果是table
{
tableNum++;
if(str1.length!=7)
{
flag2=true;
continue;
}
int num = Integer.parseInt(str1[1]);
String name = str1[3];
String phone = str1[4];
String date = str1[5];
String time = str1[6];
if(!check2(name,phone)) flag2=true;
table[tableNum] = new Table(num, name, phone, date, time);
}
//点菜
//点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数
else if (str1[0].length() == 1 && str1.length == 5) {
Sum[tableNum]++;
int num = Integer.parseInt(str1[0]);
String name = str1[1];
String taste = str1[2];
int portion = Integer.parseInt(str1[3]);
int count = Integer.parseInt(str1[4]);
recorde[tableNum][Sum[tableNum]] = new recorde(num, name, taste, portion, count);
//1 醋浇羊肉 30
}
//普通点菜
//序号、菜名、份额、份数
else if (str1[0].length() == 1 && str1.length == 4) {
Sum[tableNum]++;
int num = Integer.parseInt(str1[0]);
String name = str1[1];
int portion = Integer.parseInt(str1[2]);
int count = Integer.parseInt(str1[3]);
recorde[tableNum][Sum[tableNum]] = new recorde(num, name, portion, count);
}
//代点菜1 1 醋浇羊肉 0 1 2
else if (str1[0].length() == 1&& str1.length == 6) {
Sum[tableNum]++;
int tableNum1 = Integer.parseInt(str1[0]);
int num = Integer.parseInt(str1[1]);
String name = str1[2];
String taste = str1[3];
int portion = Integer.parseInt(str1[4]);
int count = Integer.parseInt(str1[5]);
recorde[tableNum][Sum[tableNum]] = new recorde(num, tableNum1, name, taste, portion, count);
}
//序号 delete
//删除记录格式:序号 delete
//
//标识删除对应序号的那条点菜记录。
//
//如果序号不对,输出"delete error"
else if (str1[1].equals("delete")) {
int num = Integer.parseInt(str1[0]);
if (num > Sum[tableNum] || num < 1) {
System.out.println("delete error");
exit(0);
continue;
}
for (int i = num; i < Sum[tableNum]; i++) {
recorde[tableNum][i] = recorde[tableNum][i + 1];
}
Sum[tableNum]--;
}
else if(tableNum>=1)
{
if(str1[0].length()!=1) flag3=true;if(str1[1].length()<=2) flag3=true;
String name=str1[1];
name=check1(name);
if(name==null)
{
if(str1.length!=4) flag3=true;
}
else
{
if(str1.length!=5) flag3=true;
}
}
}//计算总价
//输出
//table 1:
//1 麻婆豆腐 24
//2 油淋生菜 14
//3 麻婆豆腐 48
//table 1: 86 62 川菜 4 稍辣
//tom 13605054400 62
//2023/5/1 20/30/00
//首先输出桌号
//辣度
int spicy[] = new int[100];
/*
川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;
浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;
*/
int[] acidity = new int[100];
int[] sweetness = new int[100];String[] spicy1 = {"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"};
int[] spicyNum = new int[100];
int[] spicyNum1 = new int[100];String[] acidity1 = {"不酸", "微酸", "稍酸", "酸", "很酸"};
int[] acidityNum = new int[100];
int[] acidityNum1 = new int[100];String[] sweetness1 = {"不甜", "微甜", "稍甜", "甜"};
int[] sweetnessNum = new int[100];
int[] sweetnessNum1 = new int[100];
/* if (check(table[1])) {
System.out.println("table 1 out of opening hours");
exit(0);
}*///System.out.println("table 1:"+table[1].getDate()+" "+table[1].getTime());
boolean flag=false;
if(flag1)
{
flag=true;
System.out.print("wrong format");
}
if(flag2)
{
flag=true;
if(flag1) System.out.println();
System.out.print("wrong format");
}
if(flag3)
{
flag=true;
if(flag1||flag2) System.out.println();
System.out.print("wrong format");
}if(flag) exit(0);
for(int i=1;i<=tableNum;i++)
{
if(!check(table[i]))
{
System.out.println("table "+i+" out of opening hours");
exit(0);
}
}//输出点菜记录
int[] price = new int[100];
int[] price1 = new int[100];for (int i = 1; i <= tableNum; i++) {
System.out.println("table " + i + ":"+" ");
for (int j = 1; j <= Sum[i]; j++) {
//每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名
if(recorde[i][j].getName()!=null &&!find3(recorde[i][j].getName())){
System.out.println(recorde[i][j].getName()+" does not exist");
continue;
}if (recorde[i][j] == null)
continue;if (recorde[i][j].getTableNum1() != 0) {
int k = recorde[i][j].getTableNum1();
System.out.println(recorde[i][j].getNum() + " table " + i + " pay for table " + recorde[i][j].getTableNum1() + " " + (int) (find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5));
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1")) {
spicyNum[k] += recorde[i][j].getCount();
spicyNum1[k] += recorde[i][j].getCount();
spicy[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2")) {
acidityNum[k] += recorde[i][j].getCount();
acidityNum1[k] += recorde[i][j].getCount();
acidity[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3")) {
sweetnessNum[k] += recorde[i][j].getCount();
sweetnessNum1[k] += recorde[i][j].getCount();
sweetness[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
}
int n = (int) (1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5);
int n1= (int) ((int)(1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5) * discount(table[i], recorde[i][j].getName()) + 0.5);;
price[i] += n;
price1[i] += n1;
continue;
}//判断辣度是否超出范围
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1") && Integer.parseInt(recorde[i][j].getTaste()) > 5) {
System.out.println("spicy num out of range :" + recorde[i][j].getTaste());
continue;
}//判断酸度是否超出范围
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2") && Integer.parseInt(recorde[i][j].getTaste()) > 4) {
System.out.println("acidity num out of range :" + recorde[i][j].getTaste());
continue;
}//判断甜度是否超出范围
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3") && Integer.parseInt(recorde[i][j].getTaste()) > 3) {
System.out.println("sweetness num out of range :" + recorde[i][j].getTaste());
continue;
}
price[i] += (int) (1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5);
price1[i] += (int) ((int)(1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5) * discount(table[i],recorde[i][j].getName()) + 0.5);System.out.println(recorde[i][j].getNum() + " " + recorde[i][j].getName() + " " + (int) (find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5));
if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1")) {
spicyNum[i] += recorde[i][j].getCount();
spicyNum1[i] += recorde[i][j].getCount();
spicy[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2")) {
acidityNum[i] += recorde[i][j].getCount();
acidityNum1[i] += recorde[i][j].getCount();
acidity[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
} else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3")) {
sweetnessNum[i] += recorde[i][j].getCount();
sweetnessNum1[i] += recorde[i][j].getCount();
sweetness[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5);
}
}//判断是否有重复的用户
//如果没有重复的用户,就创建新的用户
if(!find2(table[i].name)){
user[userNum++] = new User(userNum, table[i].name, table[i].phone, price1[i]);
}else{
for(int k = 0; k < userNum; k++){
if(Objects.equals(user[k].name, table[i].name)){
user[k].price += price1[i];
break;
}
}
}
}
/*
table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
* */for (int i = 1; i <= tableNum; i++) {
if (spicyNum[i] != 0) spicy[i] = (int) (1.0*spicy[i] / spicyNum[i] + 0.5);
if (acidityNum[i] != 0) acidity[i] = (int) (1.0*acidity[i] / acidityNum[i] + 0.5);
if (sweetnessNum[i] != 0) sweetness[i] = (int) (1.0*sweetness[i] / sweetnessNum[i] + 0.5);
}
for (int i = 1; i <= tableNum; i++) {
System.out.print("table " + table[i].getNum() + ": ");
System.out.print(price[i] + " " + (price1[i]));
if (spicy[i] != 0) {
System.out.print(" " + "川菜" + " " + spicyNum1[i] + " " + spicy1[spicy[i]]);
}
if (acidity[i] != 0) {
System.out.print(" " + "晋菜" + " " + acidityNum1[i] + " " + acidity1[acidity[i]]);
}
if (sweetness[i] != 0) {
System.out.print(" " + "浙菜" + " " + sweetnessNum1[i] + " " + sweetness1[sweetness[i]]);
}
System.out.println();
}
//最后按name顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。
//排序
for (int i = 0; i < userNum; i++) {
for (int j = i + 1; j < userNum; j++) {
if (user[i].getName().compareTo(user[j].getName()) > 0) {
User temp = user[i];
user[i] = user[j];
user[j] = temp;
}
}
}//输出
for (int i = 0; i < userNum-1; i++) {
System.out.println(user[i].getName() + " " + user[i].getPhone() + " " + user[i].getPrice());
}
System.out.print(user[userNum-1].getName() + " " + user[userNum-1].getPhone() + " " + user[userNum-1].getPrice());}
public static String check1(String name) {
name = find1(name);if(name==null)
return null;if (name.equals("川菜"))
return "1";
if (name.equals("晋菜"))
return "2";
if (name.equals("浙菜"))
return "3";
return null;
}public static boolean find3(String name)
{
for(int i =1; i <= dishNum; i++)
{
if(Objects.equals(dish[i].getName(), name))
return true;
}
return false;
}//约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。
public static boolean check2(String name, String phone) {
if (name.length() > 10)
return false;
if (phone.length() != 11)
return false;
if (phone.charAt(0) != '1')
return false;
if (phone.charAt(1) != '8' && phone.charAt(1) != '3')
return false;
if (phone.charAt(1) == '8' && phone.charAt(2) != '0' && phone.charAt(2) != '1' && phone.charAt(2) != '9')
return false;
if (phone.charAt(1) == '3' && phone.charAt(2) != '3' && phone.charAt(2) != '5' && phone.charAt(2) != '6')
return false;
return true;
}public static boolean find2(String name) {
for (int i = 0; i < userNum; i++) {
if (user[i].getName().equals(name))
return true;
}
return false;
}public static String find1(String name) {
for (int i = 1; i <= dishNum; i++) {
if (dish[i].getName().equals(name))
return dish[i].getTaste();
}
return null;
}public static int find4(String name)
{
for(int i=1;i<=dishNum;i++)
{
if(dish[i].getName()==null)
return -1;
if(Objects.equals(dish[i].getName(),name))
return i;
}
return -1;
}//桌号时间超出营业范围。例如:
public static boolean check(Table table) {
int year = Integer.parseInt(table.date.split("/")[0]);
int month = Integer.parseInt(table.date.split("/")[1]);
int day = Integer.parseInt(table.date.split("/")[2]);
int hour = Integer.parseInt(table.time.split("/")[0]);
int minute = Integer.parseInt(table.time.split("/")[1]);
int second = Integer.parseInt(table.time.split("/")[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
int week = calendar.get(Calendar.DAY_OF_WEEK);
if (week == 3 || week == 4) {
if(hour >=10 && hour <=20) return true;
if(hour==9&&minute>=30) return true;
return hour == 21 && minute <= 30;
} else {
if(hour >=10 && hour <=14) return true;
if(hour >=17 && hour <=19) return true;
if(hour==9&&minute>=30) return true;
return hour == 20 && minute <= 30;
}}
//计算折扣
//折扣的计算方法(注:以下时间段均按闭区间计算):
//周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。//周末全价,营业时间:9:30-21:30
//判断是周几,然后打几折
public static double discount(Table table,String name){int year = Integer.parseInt(table.date.split("/")[0]);
int month = Integer.parseInt(table.date.split("/")[1]);
int day = Integer.parseInt(table.date.split("/")[2]);
int hour = Integer.parseInt(table.time.split("/")[0]);
int minute = Integer.parseInt(table.time.split("/")[1]);
int second = Integer.parseInt(table.time.split("/")[2]);Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
int week = calendar.get(Calendar.DAY_OF_WEEK);name=check1(name);
if((name!=null)&&(name.equals("1")||name.equals("2")||name.equals("3")))
{
if(week==3||week==4) return 1.0;
else return 0.7;
}
else
{
if(week==3||week==4) return 1.0;
else
{
if(hour>=17&&hour<=19) return 0.8;
if(hour==20&&minute<=30) return 0.8;
if(hour>=11&&hour<=13) return 0.6;
if(hour==10&&minute>=30) return 0.6;
if(hour==14&&minute<=30) return 0.6;
}
}return 1.0;
}public static double prices(int num) {
if (num == 1) return 1.0;
if (num == 2) return 1.5;
if (num == 3) return 2.0;
return 0;
}public static int find(String name) {
for (int i = 1; i <= dishNum; i++) {
if (dish[i].getName().equals(name))
return dish[i].getPrice();
} -
![]()
也是拿下了62分,非常开心,加油。
- 期中考试也是拿下好吧
- import java.util.Scanner;
class Circle {
private double r;
public Circle(){
}
public double getbanjing() {
return r;
}
public void setbanjing(double r) {
this.r = r;
}
public double getmianji() {
return 3.141592654*(r*r);
}
}
public class Main{
public static void main(String[] args){
Scanner cao=new Scanner(System.in);
double r = cao.nextDouble();
Circle circle = new Circle();
circle.setbanjing(r);
// double rr=circle.getbanjing()*3.14159*circle.getbanjing();
if(r>0)
System.out.println(String.format("%.2f",circle.getmianji()));
else
System.out.println("Wrong Format");
}
} - import java.util.Scanner;
c
public class Main{
public static void main(String[] args){
Scanner cao=new Scanner(System.in);
double x1=cao.nextDouble();
double y1=cao.nextDouble();
double x2=cao.nextDouble();
double y2=cao.nextDouble();
Point b= new Point(x2,y2);
Point a= new Point(x1,y1);
Rectangle juxing = new Rectangle(a,b);
double m;
m=juxing.getmianji();
if(m>=0)
System.out.println(String.format("%.2f",juxing.getmianji()));
if(m<0)
System.out.println(String.format("%.2f",-1*juxing.getmianji()));
}
} - import java.util.Scanner;
import java.util.*;
class Shape implements Comparable{
double Area;
int i=1;
public Shape(){
}
public double getArea(){
return Area;
}
}
class Circle extends Shape {
private double r;
public Circle(){
}
public Circle(double r){
this.r = r;
}
public double getbanjing() {
return r;
}
public void setbanjing(double r) {
this.r = r;
}
public double getArea() {
if(r<=0)
return -1;
return 3.141592654*(r*r);
}
}
class Rectangle extends Shape{
private Point b;
private Point a;
public Rectangle(Point a,Point b)
{
this.a=a;
this.b=b;
}
public double getArea() {
return (b.getx()-a.getx())*(b.gety()-a.gety());
}
}
class Point{
private double x;
private double y;
public Point(double x,double y){
this.x=x;
this.y=y;
}
public double getx(){
return x;
}
public double gety(){
return y;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
ArrayList<Shape> list = new ArrayList<>();
int choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1://Circle
double radiums = input.nextDouble();
Shape circle = new Circle(radiums);
list.add(circle);
break;
case 2://Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point leftTopPoint = new Point(x1,y1);
Point lowerRightPoint = new Point(x2,y2);
Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
list.add(rectangle);
break;
}
choice = input.nextInt();
}
list.sort(Comparator.naturalOrder());//正向排序
for(int i = 0; i < list.size(); i++) {
System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
}
}
} - import java.util.Scanner;
class Shape{
double Area;
int i=1;
public Shape(){
}
public double getArea(){
return Area;
}
}
class Circle extends Shape {
private double r;
public Circle(){
}
public Circle(double r){
this.r = r;
}
public double getbanjing() {
return r;
}
public void setbanjing(double r) {
this.r = r;
}
public double getArea() {
if(r<=0)
return -1;
return 3.141592654*(r*r);
}
}
class Rectangle extends Shape{
private Point b;
private Point a;
public Rectangle(Point a,Point b)
{
this.a=a;
this.b=b;
}
public double getArea() {
return (b.getx()-a.getx())*(b.gety()-a.gety());
}
}
class Point{
private double x;
private double y;
public Point(double x,double y){
this.x=x;
this.y=y;
}
public double getx(){
return x;
}
public double gety(){
return y;
}
}
class Main{
public static void printArea(Shape a){
if(a.getArea()==-1)
System.out.println("Wrong Format");
else
System.out.println(String.format("%.2f",a.getArea()));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
switch(choice) {
case 1://Circle
double radiums = input.nextDouble();
Shape circle = new Circle(radiums);
printArea(circle);
break;
case 2://Rectangle
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
Point leftTopPoint = new Point(x1,y1);
Point lowerRightPoint = new Point(x2,y2);
Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
printArea(rectangle);
break;
}
}
} - ok期中考试也是拿下,拜拜。



浙公网安备 33010602011771号