jmu-PTA Java答案汇总(中)
2023-12-15 22:20:31
目录
7-52 sdut-String-3 字符串字母大小写转换逆序输出
Java练习第四章
6-40 创建一个直角三角形类实现IShape接口
class RTriangle implements IShape {
double a,b;
public RTriangle(double a, double b) {
this.a = a;
this.b = b;
}
@Override
public double getArea() {
return (a * b) / 2;
}
@Override
public double getPerimeter() {
return a + b + (Math.sqrt(a * a + b * b));
}
}
6-41 Animal抽象类和IAbility接口
class Dog implements IAbility {
String name;
int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void showInfo() {
System.out.println("我是一只狗,我的名字是"+name+",今年"+age+"岁");
}
@Override
public void cry() {
System.out.println("旺旺");
}
}
class Cat implements IAbility {
String name;
int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public void showInfo() {
System.out.println("我是一只猫,我的名字是"+name+",今年"+age+"岁");
}
@Override
public void cry() {
System.out.println("喵喵");
}
}
class Simulator {
public void playSound(IAbility animal) {
animal.showInfo();
animal.cry();
}
}
6-42 设计门票(抽象类)
abstract class Ticket {
private int number;
public Ticket(int number) {
this.number = number;
}
public abstract int getPrice();
public abstract String toString();
public int getNumber() {
return number;
}
}
class WalkupTicket extends Ticket {
public WalkupTicket(int number) {
super(number);
}
@Override
public int getPrice() {
return 50;
}
@Override
public String toString() {
return "Number:"+getNumber()+",Price:"+getPrice();
}
}
class AdvanceTicket extends Ticket {
private int leadTime;
public AdvanceTicket(int number, int leadTime) {
super(number);
this.leadTime = leadTime;
}
@Override
public int getPrice() {
if(leadTime > 10)
return 30;
else
return 40;
}
@Override
public String toString() {
return "Number:"+getNumber()+",Price:"+getPrice();
}
public int getLeadTime() {
return leadTime;
}
}
class StudentAdvanceTicket extends AdvanceTicket {
private int height;
public StudentAdvanceTicket(int number, int leadTime, int height) {
super(number, leadTime);
this.height = height;
}
@Override
public int getPrice() {
if(height > 120) {
if(getLeadTime() > 10)
return 20;
else
return 30;
}
else {
if(getLeadTime() > 10)
return 10;
else
return 15;
}
}
@Override
public String toString() {
return "Number:"+getNumber()+",Price:"+getPrice();
}
}
6-43 Animal动物工厂
class Dog extends Animal {
int violence;
public Dog(String name, int age, int violence) {
super(name, age);
this.violence = violence;
}
public Dog(String name, int age) {
super(name, age);
}
@Override
public void info() {
System.out.println("该狗的名字是"+getName()+",年龄是"+getAge()+"岁,凶猛程度是"+violence+"度");
}
@Override
public void speak() {
System.out.println("旺旺");
}
}
class Cat extends Animal {
int mousingAbility;
public Cat(String name, int age, int mousingAbility) {
super(name, age);
this.mousingAbility = mousingAbility;
}
@Override
public void info() {
System.out.println("该猫的名字是"+getName()+",年龄是"+getAge()+"岁,捕鼠能力是"+mousingAbility+"分");
}
@Override
public void speak() {
System.out.println("喵喵");
}
}
class Factory {
public Animal getAnimalByType(int type,String name, int age, int ownAttribute) {
if(type == 1)
return new Dog(name,age,ownAttribute);
else
return new Cat(name,age,ownAttribute);
}
}
6-44 水果接口-工厂设计模式
class Apple implements Fruit {
@Override
public void eat() {
System.out.println("吃苹果啦啦啦!!");
}
}
class Orange implements Fruit {
@Override
public void eat() {
System.out.println("吃橘子啦啦啦!!");
}
}
class Factory {
public static Fruit getInstance(String name) {
if(name.equals("apple"))
return new Apple();
if(name.equals("orange"))
return new Orange();
else
return null;
}
}
6-45 工厂设计模式-运输工具
class Train implements Motor {
@Override
public void transport() {
System.out.println("车次为30,节数为:20的火车开始进行货物运输。");
}
}
class Car implements Motor {
@Override
public void transport() {
System.out.println("汽车开始进行货物运输。");
}
}
class Tractor implements Motor {
@Override
public void transport() {
System.out.println("拖拉机在耕地");
System.out.println("拖拉机开始进行货物运输。");
}
}
class Factory {
public static Motor createMotor(String name) {
if(name.equals("火车"))
return new Train();
else if(name.equals("汽车"))
return new Car();
else if(name.equals("拖拉机"))
return new Tractor();
else
return null;
}
}
6-46 手机接口-适配器模式
abstract class PhoneAdapter implements Phone {
public void dial (){}
public void location() {}
public void photo() {}
public void music() {}
public void video() {}
}
class HuawPhone extends PhoneAdapter {
@Override
public void dial() {
System.out.println("华为手机拨打电话。");
}
@Override
public void photo() {
System.out.println("华为手机拍照。");
}
@Override
public void music() {
System.out.println("华为手机播放音乐。");
}
@Override
public void video() {
System.out.println("华为手机播放视频。");
}
}
class OldPhone extends PhoneAdapter {
@Override
public void dial() {
System.out.println("老人机拨打电话。");
}
@Override
public void location() {
System.out.println("老人机定位。");
}
}
6-47 购买汽车-代理模式
class People implements Buy_car {
private int cash;
private boolean vip;
private String username;
public People(int cash,boolean vip,String username) {
this.cash = cash;
this.vip = vip;
this.username = username;
}
@Override
public void buy_mycar() {
System.out.println(username + "要买车");
}
public int getCash() {
return cash;
}
public boolean isVip() {
return vip;
}
public String getUsername() {
return username;
}
}
class ProxySale implements Buy_car {
private People people;
public ProxySale(People people) {
this.people = people;
}
@Override
public void buy_mycar() {
if(people.isVip()) {
System.out.println(people.getUsername() + "是vip,可以直接购买新车!");
} else if (people.getCash() >= 50000) {
System.out.println(people.getUsername() + "买了新车,交易结束!");
} else {
System.out.println(people.getUsername() + "钱不够,不能购买新车!");
}
}
}
6-48 使用面向对象的思想编写程序描述动物
interface Swimmable {
void swim();
}
interface Flyable {
void fly();
}
class Rabbit extends Animal {
public Rabbit(String name, String color, String type) {
super(name, color, type);
}
@Override
public void shout() {
System.out.println("那只"+getColor()+"的,名字叫"+getName()+"的兔子正在叽叽的叫");
}
@Override
public void eat() {
System.out.println("兔子是哺乳类,爱吃胡萝卜");
}
}
class Frog extends Animal implements Swimmable {
public Frog(String name, String color, String type) {
super(name, color, type);
}
@Override
public void shout() {
System.out.println("那只" + getColor() + "的,名字叫" + getName() + "的青蛙正在呱呱的叫");
}
@Override
public void eat() {
System.out.println("青蛙是非哺乳类,爱吃昆虫");
}
@Override
public void swim() {
System.out.println("青蛙是游泳高手");
}
}
class Eagle extends Animal implements Flyable {
public Eagle(String name, String color, String type) {
super(name, color, type);
}
@Override
public void shout() {
System.out.println("那只" + getColor() + "的,名字叫" + getName() + "的老鹰正在啾啾的叫");
}
@Override
public void eat() {
System.out.println("老鹰是非哺乳类,爱吃小鸡");
}
@Override
public void fly() {
System.out.println("老鹰是飞翔高手");
}
}
6-49 超市购物
class Book implements Goods {
String name;
double price;
public Book(String name,double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class Drink implements Goods {
String name;
double price;
public Drink(String name,double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class ShopCar {
Goods[] goods;
static int foot = 0;
public ShopCar(int num) {
goods = new Goods[num];
}
public void add(Goods good) {
if(foot < this.goods.length) {
this.goods[foot] = good;
foot++;
System.out.println("添加成功!");
}
else {
System.out.println("添加失败!");
return;
}
}
public void remove(int key) {
if(key < this.goods.length && key >= 0) {
for (int i = key; i < foot - 1; i++) {
goods[i] = goods[i+1];
}
foot--;
this.goods[goods.length-1] = null;
}
}
public double check() {
double sum = 0;
for (int i = 0; i < foot; i++) {
sum += goods[i].getPrice();
}
return sum;
}
public Goods[] getGoods() {
return goods;
}
}
7-41 接口--四则计算器
import java.util.Scanner;
class ICompute {
public int computer(int n, int m) {
return 0;
}
}
class Add extends ICompute {
@Override
public int computer(int n, int m) {
return m+n;
}
}
class Sub extends ICompute {
@Override
public int computer(int n, int m) {
return n - m;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
Add add = new Add();
Sub sub = new Sub();
System.out.println(add.computer(a, b));
System.out.println(sub.computer(a, b));
}
}
Java练习第五章
7-51 判断登录信息是否正确--字符串比较
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = scan.next();
String set = scan.next();
String key_name = "SwpuIot";
String key_set = "123456";
if(key_set.equals(set)&&key_name.equals(name))
System.out.println("Welcome");
else if(key_set.equals(set)==false&&key_name.equals(name)==true)
System.out.println("Mismatch");
else
System.out.println("NotExist");
}
}
7-52 sdut-String-3 字符串字母大小写转换逆序输出
import java.util.Scanner;
public class Main {
public static String reverse(String str) {
return new StringBuffer(str).reverse().toString();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
char ch[] = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if(ch[i]>='a'&&ch[i]<='z')
ch[i] -= 32;
else if(ch[i]>='A'&&ch[i]<='Z')
ch[i] += 32;
}
str = new String(ch);
System.out.println(reverse(str));
}
}
7-53 身份证排序
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static String sub(String buf,int start,int end) {
return new StringBuffer(buf).substring(start, end).toString();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] id = new String[n];
String[] buf = new String[n];
for(int i = 0;i < n;i++) {
id[i] = scan.next();
buf[i] = id[i].substring(6,10)+"-"
+id[i].substring(10,12)+"-"
+id[i].substring(12,14);
}
Arrays.sort(buf);
for(String b:buf) {
System.out.println(b);
}
}
}
7-54 StringBuffer-拼接字符串
import java.util.Scanner;
public class Main {
public static void main(String[]args) {
StringBuffer buf = new StringBuffer();
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int begin = scan.nextInt();
int end = scan.nextInt();
for(int i = 0;i<n;i++) {
buf.append(i);
}
String ret = buf.subSequence(begin, end).toString();
System.out.println(ret);
}
}
7-55 判断指定字符串是否合法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String S = scan.next();
if(S.matches("[A-Z]"+"[a-z]{3}"+"\\d{3}"))
System.out.println(S + "合法");
else
System.out.println(S + "不合法");
}
}
7-56 判断Email地址的合法性
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String S = scan.next();
String[] str = S.split("\\.");
if(str[1].matches("(com)|(cn)")) {
System.out.println(S+"是正确的Email地址");
return;
}
System.out.println(S+"不是正确的Email地址");
}
}
7-57 使用正则表达式判断IP地址
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String S = scan.next();
boolean flag = S.matches("[0-2][0-5][0-5]\\." +
"[0-2][0-5][0-5]\\." +
"[0-2][0-5][0-5]\\." +
"[0-2][0-5][0-5]");
if(flag) {
System.out.println(S + "是合法的IP地址");
}
else {
System.out.println(S + "不是合法的IP地址");
}
}
}
6-51(处理数组、字符串) 人口统计
public static int numofHan(String data[]) {
int sum = 0;
for (int i = 0; i < data.length; i++) {
String s = data[i].toString();
String ch[] = s.split(",",4);
if(ch[ch.length-1].equals("汉族")||ch[ch.length-1].equals("汉族人"))
sum++;
}
return sum;
}
文章来源:https://blog.csdn.net/linsc_05/article/details/135023970
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!