140 - 家电类
Description
某大型家电企业拥有一批送货卡车,运送电视机、洗衣机、空调等家电。编程计算每个卡车所装载货物的总重量。
要求有一个Appliance(家电)接口和有三个实现类TV、WashMachine和AirConditioner,这些类能够提供自重。
有一个Truck类,包含了该货车上的所有家电,用一个集合(数组或集合类)表示。
Main函数中程序能够输出Truck类所装载货物的总重量。
Input
家电数量
家电种类编号 家电重量
注意:各个家电的编号为:TV:1 WashMachine:2 AirConditioner:3
Output
总重量
Sample Input
5
1 20
2 30
3 25
3 30
2 40
Sample Output
145
MyAnswer
import java.util.*;
public class Main {
public static void main(String[] args) {
Truck truck = new Truck();
truck.getScan();
System.out.println(truck.getSumWeight());
}
}
interface Appliance{
public int getWeight();
}
class App implements Appliance{
int weight;
public int getWeight(){
return 0;
}
}
class TV extends App implements Appliance{
TV(int w) {
weight=w;
}
@Override
public int getWeight() {
return weight;
}
}
class WashMachine extends App implements Appliance{
WashMachine(int w) {
weight=w;
}
@Override
public int getWeight() {
return weight;
}
}
class AirConditioner extends App implements Appliance{
AirConditioner(int w) {
weight=w;
}
@Override
public int getWeight() {
return weight;
}
}
class Truck{
int num;
App[] app;
int SumWeight;
public void getScan(){
Scanner scan = new Scanner(System.in);
num = scan.nextInt();
app = new App[num];
for(int i=0; i<num; i++){
int type = scan.nextInt();
int w = scan.nextInt();
if(type == 1)
app[i] = new TV(w);
else if(type == 2)
app[i] = new WashMachine(w);
else if(type == 3)
app[i] = new AirConditioner(w);
}
}
public int getSumWeight() {
for(int i=0; i<num; i++){
SumWeight += app[i].getWeight();
}
return SumWeight;
}
}
150 - 教师类
Description
设计一个教师类Teacher,要求:
属性有编号(int no)、姓名(String name)、年龄(int age)、所属学院(String seminary),为这些属性设置相应的get和set方法。
为Teacher类重写equals方法,要求:当两个教师对象的no相同时返回true。
重写Teacher类的toString方法,通过该方法可以返回“no: **, name:**, age: **, seminary: **”形式的字符串。
Input
两个教师对象的编号,姓名,年龄,学院
Output
教师的信息
两个教师是否相等
Sample Input
1 Linda 38 SoftwareEngineering
2 Mindy 27 ComputerScience
Sample Output
no: 1, name:Linda, age: 38, seminary: SoftwareEngineering
no: 2, name:Mindy, age: 27, seminary: ComputerScience
false
MyAnswer
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n1 = scan.nextInt();
String na1 = scan.next();
int a1 = scan.nextInt();
String s1 = scan.next();
Teacher t1 = new Teacher(n1,na1,a1,s1);
int n2 = scan.nextInt();
String na2 = scan.next();
int a2 = scan.nextInt();
String s2 = scan.next();
Teacher t2 = new Teacher(n2,na2,a2,s2);
System.out.println(t1.toString());
System.out.println(t2.toString());
System.out.println(t1.equals(t2));
}
}
class Teacher{
int no;
String name;
int age;
String seminary;
Teacher(int no, String name, int age, String seminary){
setNo(no);
setName(name);
setSeminary(seminary);
setAge(age);
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public int getAge() {
return age;
}
public String getSeminary() {
return seminary;
}
public boolean equals(Object o){
if(o == null)
return false;
else{
boolean res = false;
if(o instanceof Teacher){
Teacher t = (Teacher)o;
if(this.no == t.no){
res = true;
}
}
return res;
}
}
public String toString(){
return "no: "+getNo()+", name:"+getName()+", age: "+getAge()+", seminary: "+getSeminary();
}
}
149 - 教师类-2
Description
修改题目143
1. 修改教师类,使得由多个Teacher对象所形成的数组可以排序(编号由低到高排序),并在main函数中使用Arrays.sort(Object[] a)方法排序
2. 定义一个类TeacherManagement,包含教师数组,提供方法add(Teacher[]),使其可以添加教师,提供重载方法search,方法可以在一组给定的教师中,根据姓名或年龄返回等于指定姓名或年龄的教师的字符串信息,信息格式为:“no: **, name:**, age: **, seminary: **”。如果没有满足条件的教师,则返回“no such teacher”。
Input
教师个数
教师信息
待查找教师的姓名
待查找教师的年龄
Output
排序后的信息
按姓名查找的老师信息
按年龄查找的老师信息
Sample Input
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
Sample Output
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
MyAnswer
import java.util.*;
import java.lang.reflect.Array;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i;
int num = scan.nextInt();
TeacherManagement tm = new TeacherManagement(num);
Teacher[] t = new Teacher[num];
for(i=0; i<num; i++){
int no = scan.nextInt();
String name = scan.next();
int age = scan.nextInt();
String sem = scan.next();
t[i] = new Teacher(no,name,age,sem);
}
tm.add(t);
Arrays.sort(t);
for(i=0;i<t.length;i++){
System.out.println(t[i].toString());
}
String s1 = scan.next();
tm.search(s1);
int s2 = scan.nextInt();
tm.search(s2);
}
}
class Teacher implements Comparable<Teacher>{
int no;
String name;
int age;
String seminary;
Teacher(int no, String name, int age, String seminary){
setNo(no);
setName(name);
setSeminary(seminary);
setAge(age);
}
public void setNo(int no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSeminary(String seminary) {
this.seminary = seminary;
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public int getAge() {
return age;
}
public String getSeminary() {
return seminary;
}
public boolean equals(Object o){
if(o == null)
return false;
else{
boolean res = false;
if(o instanceof Teacher){
Teacher t = (Teacher)o;
if(this.no == t.no){
res = true;
}
}
return res;
}
}
public String toString(){
return "no: "+getNo()+", name: "+getName()+", age: "+getAge()+", seminary: "+getSeminary();
}
public int compareTo(Teacher t){
if(this.no == t.no)
return 0;
else if(this.no>t.no)
return 1;
else
return -1;
}
}
class TeacherManagement{
Teacher[] t;
int num;
TeacherManagement(int num){
this.num = num;
this.t = new Teacher[num];
}
public void add(Teacher[] t){
this.t = t;
}
// 重载
public void search(int age){
int flag = 0;
System.out.println("search by age:");
for (Teacher aT : t) {
if (aT.age == age) {
flag = 1;
System.out.println(aT.toString());
}
}
if(flag==0){
System.out.println("no such teacher");
}
}
public void search(String name){
int flag = 0;
System.out.println("search by name:");
for (Teacher aT : t) {
if (aT.name.equals(name)) {
flag = 1;
System.out.println(aT.toString());
}
}
if(flag==0){
System.out.println("no such teacher");
}
}
}
142 - 计算机类
Description
构造计算机类,其中包含其配置信息:处理器、主板、内存、显示器、硬盘等设备,各个设备均有型号(字符串),
特别的,处理器有主频(小数)和内核数(整数)、显示器有尺寸(整型)、内存和硬盘有容量数据(GB为单位)。
请你尝试构造合适的类和类的关系来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。
重写相关类的equals方法,使得两个配置完全相同的计算机为相同的计算机。重写相关类的toString函数,打印计算机的配置信息。
在main函数中
Input
两个计算机对象,包含
CPU:型号、主频、内核
主板:型号
内存:容量
显示器:尺寸
硬盘:容量
Output
两个对象是否相等
两个对象的配置信息
Sample Input
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048
Corei7 2.8 4
GIGABYTE-B250M-D3H
xiede-DDR3 8
SamsungC27F39 27
SEAGATE-ST1000DM010 2048
Sample Output
true
Computer1:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048
Computer2:
CPU:
Model: Corei7
Frequency: 2.8
Number of Cores: 4
Mainboard:
Model: GIGABYTE-B250M-D3H
Memory:
Model: xiede-DDR3
Size: 8
Screen:
Model: SamsungC27F39
Size: 27
Harddisk:
Model: SEAGATE-ST1000DM010
Size: 2048
HINT
为计算机类和各个组成配件都构造类,分别重写toString和equals方法
在计算机类中调用各个配件的equals和toString方法
回车用\n
小数用String.format("%.1f",1.234)
MyAnswer
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String cModel = scan.next();
String cFrequency = scan.next();
int cNumofCores = scan.nextInt();
String mModel = scan.next();
String meModel = scan.next();
int meSize = scan.nextInt();
String sModel = scan.next();
int sSize = scan.nextInt();
String hModel = scan.next();
int hSize = scan.nextInt();
CPU cpu1 = new CPU(cModel,cFrequency,cNumofCores);
MainBoard mb1 = new MainBoard(mModel);
Memory mem1 = new Memory(meModel,meSize);
Screen sc1 = new Screen(sModel,sSize);
Harddisk hd1 = new Harddisk(hModel,hSize);
Computer cp1 = new Computer(cpu1,mb1,mem1,sc1,hd1);
cModel = scan.next();
cFrequency = scan.next();
cNumofCores = scan.nextInt();
mModel = scan.next();
meModel = scan.next();
meSize = scan.nextInt();
sModel = scan.next();
sSize = scan.nextInt();
hModel = scan.next();
hSize = scan.nextInt();
CPU cpu2 = new CPU(cModel,cFrequency,cNumofCores);
MainBoard mb2 = new MainBoard(mModel);
Memory mem2 = new Memory(meModel,meSize);
Screen sc2 = new Screen(sModel,sSize);
Harddisk hd2 = new Harddisk(hModel,hSize);
Computer cp2 = new Computer(cpu2,mb2,mem2,sc2,hd2);
System.out.println(cp1.equals(cp2));
System.out.println("Computer1:");
System.out.println(cp1);
System.out.println("Computer2:");
System.out.println(cp2);
}
}
class Father{
String model;
Father(String model){
this.model = model;
}
}
class CPU extends Father{
String freq;
int numCores;
public CPU(String model, String freq,int numCores){
super(model);
this.freq = freq;
this.numCores = numCores;
}
public boolean equals(Object o){
if(o==null) return false;
else if(o instanceof CPU){
CPU c = (CPU) o;
return this.model.equals(c.model) && this.freq.equals(c.freq) && this.numCores == c.numCores;
}
else
return false;
}
public String toString(){
return "CPU:\nModel: " + model + "\nFrequency: " +freq +"\nNumber of Cores: " + numCores + "\n";
}
}
class MainBoard extends Father{
MainBoard(String model){
super(model);
}
public boolean equals(Object o){
if(o==null) return false;
else if(o instanceof MainBoard){
MainBoard mb = (MainBoard) o;
return this.model.equals(mb.model);
}
else
return false;
}
public String toString(){
return "Mainboard:\nModel: "+model+"\n";
}
}
class Memory extends Father{
int size;
Memory(String model, int size){
super(model);
this.size = size;
}
public boolean equals(Object o){
if(o==null) return false;
else if(o instanceof Memory){
Memory m = (Memory) o;
return this.model.equals(m.model) && this.size == m.size;
}
else
return false;
}
public String toString(){
return "Memory:\nModel: "+model+"\nSize: "+size+"\n";
}
}
class Screen extends Father{
int size;
Screen(String model, int size){
super(model);
this.size = size;
}
public boolean equals(Object o){
if(o==null) return false;
else if(o instanceof Screen){
Screen s = (Screen) o;
return this.model.equals(s.model) && this.size == s.size;
}
else
return false;
}
public String toString(){
return "Screen:\nModel: "+model+ "\nSize: "+size+"\n";
}
}
class Harddisk extends Father{
int size;
Harddisk(String model, int size){
super(model);
this.size = size;
}
public boolean equals(Object o){
if(o==null) return false;
else if(o instanceof Harddisk){
Harddisk h = (Harddisk) o;
return this.model.equals(h.model) && this.size == h.size;
}
else
return false;
}
public String toString(){
return "Harddisk:\nModel: "+model+ "\nSize: "+size;
}
}
class Computer{
CPU cpu;
MainBoard mainboard;
Memory memory;
Screen screen;
Harddisk harddisk;
Computer(CPU cpu,MainBoard mainboard,Memory memory,Screen screen,Harddisk harddisk){
this.cpu = cpu;
this.mainboard = mainboard;
this.memory = memory;
this.screen = screen;
this.harddisk = harddisk;
}
public boolean equals(Object o){
if(o==null) return false;
else if(o instanceof Computer){
Computer c = (Computer) o;
return this.cpu.equals(c.cpu) && this.mainboard.equals(c.mainboard) && this.memory.equals(c.memory) && this.screen.equals(c.screen) && this.harddisk.equals(c.harddisk);
}
else
return false;
}
public String toString() {
return cpu.toString() + mainboard.toString() + memory.toString() + screen.toString() + harddisk.toString();
}
}
139 - 整数数组比较
Description
给定两个整型数组A和B,将A的元素复制到B中,使得两个数组完全相同。再将B数组从小到大排列,将两数组的同一位
置上对应的元素进行比较,统计出A中大于B的元素个数,等于B中元素的个数,小于B中的元素的个数。
Input
数组A的个数
数组A元素
Output
A大于B的个数
A等于B的个数
A小于B的个数
Sample Input
10
23 1 32 87 65 12 21 9 76 45
Sample Output
4
1
5
HINT
可用Arrays.sort排序
MyAnswer
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int[] a = new int[num];
int i;
int bigger=0,smaller=0,equal=0;
for(i=0; i<num; i++){
a[i] = scan.nextInt();
}
int b[] = a.clone();
Arrays.sort(b);
for(i=0; i<num; i++){
if(a[i] > b[i])
bigger++;
else if(a[i]==b[i])
equal++;
else
smaller++;
}
System.out.println(bigger);
System.out.println(equal);
System.out.println(smaller);
}
}
151 - 矩阵类
Description
利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:
(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;
(2)get(int row,int col):取第row行第col列的元素;
(3)width():返回矩阵的列数;
(4)height():返回矩阵的行数;
(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;
(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。
(7)Matrix transpose():返回当前矩阵的转置矩阵;
(8)toString():以行和列的形式打印出当前矩阵。
Input
矩阵的行列数
矩阵的数据
设置矩阵值的行、列和值
获取矩阵值的行、列
待相加矩阵的行列数
待相加矩阵的值
待相乘矩阵的行列数
待相乘矩阵的值
Output
矩阵的行、列数
设置矩阵值后的矩阵
某行某列的矩阵值
矩阵相加结果
矩阵相乘结果
矩阵转置结果
Sample Input
3 3
1 2 3
4 5 6
7 8 9
2 3 8
1 3
3 3
1 2 3
4 5 6
7 8 9
3 2
1 2
1 2
1 2
Sample Output
row:3 column:3
after set value:
1 2 3
4 5 8
7 8 9
value on (1,3):3
after add:
2 4 6
8 10 14
14 16 18
after multiply:
6 12
17 34
24 48
after transpose:
1 4 7
2 5 8
3 8 9
MyAnswer
import java.rmi.MarshalException;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
int col = scan.nextInt();
System.out.println("row:" + row + " column:" + col);
Matrix matrix = new Matrix(row,col);
for(int i=0; i<row; i++){
for (int j = 0; j < col; j++) {
double t = scan.nextDouble();
matrix.set(i,j,t);
}
}
System.out.println("after set value:");
int m = scan.nextInt();
int n = scan.nextInt();
double p = scan.nextDouble();
matrix.set(m-1,n-1,p);
System.out.println(matrix);
m = scan.nextInt();
n = scan.nextInt();
System.out.println("value on (" + m + "," + n + "):" + (int)matrix.get(m - 1,n - 1));
System.out.println("after add:");
m = scan.nextInt();
n = scan.nextInt();
Matrix matrix2 = new Matrix(m,n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
double t = scan.nextDouble();
matrix2.set(i,j,t);
}
}
System.out.println(matrix2.add(matrix));
System.out.println("after multiply:");
m = scan.nextInt();
n = scan.nextInt();
Matrix matrix3 = new Matrix(m,n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
double t = scan.nextDouble();
matrix3.set(i,j,t);
}
}
System.out.println(matrix.multiply(matrix3));
System.out.println("after transpose:");
System.out.println(matrix.transpose());
scan.close();
}
}
class Matrix{
int row;
int col;
double a[][];
public Matrix(int r, int c){
row = r;
col = c;
a = new double[row][col];
}
public int getRow(){
return row;
}
public int getCol(){
return col;
}
public void set(int r, int c, double value){
this.a[r][c] = value;
}
public double get(int r, int c){
return this.a[r][c];
}
public Matrix add(Matrix b){
for(int i=0; i<b.getRow(); i++){
for(int j=0; j<b.getCol(); j++){
double tmp=0;
tmp += this.get(i,j) + b.get(i,j);
this.set(i,j,tmp);
}
}
return this;
}
public Matrix multiply(Matrix b){
Matrix c = new Matrix(this.getRow(),b.getCol());
for(int i=0; i<this.getRow(); i++){
for(int j=0; j<b.getCol(); j++){
double tmp=0;
for(int k=0; k<this.getCol(); k++){
tmp += this.get(i,k)*b.get(k,j);
}
c.set(i,j,tmp);
}
}
return c;
}
public Matrix transpose(){
for(int i=0; i<this.getRow(); i++){
for(int j=0; j<i; j++){
double tmp = this.get(i,j);
this.set(i,j,this.get(j,i));
this.set(j,i,tmp);
}
}
return this;
}
public String toString(){
String s = "";
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
if(j != 0)
s += " ";
int tmp = (int)this.get(i,j);
s += tmp;
}
if(i != row-1)
s += "\n";
}
return s;
}
}