Java第四次作业

140 - 家电类

Time Limit: 1000  Memory Limit: 65535

Submit: 105  Solved: 69

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

_____________

import java.util.*;

public class Main{

    public static void main(String[] args) {

        Truck t=new Truck();

        Scanner scan = new Scanner(System.in);     

        int num= scan.nextInt();

        int type;

        int weight;

        for(int i=0;i

        {

        type = scan.nextInt();

        weight = scan.nextInt();

            t.getWeight(weight);

            t.sortit(type,weight);


        }

        t.getSum();

        scan.close();

    }

}

interface Appliance

{

  void setWeight(int b);

}

class Truck{

    public int sumweight;

    public void getWeight(int a){

        this.sumweight+=a;

    }

    public void sortit(int t,int w) {

    if(t==1)

        {

            TV x=new TV(w);

        }

        else if(t==2)

        {

            WashMachine x=new WashMachine(w);

        }

        else if(t==3)

        {

            AirConditioner x=new AirConditioner(w);

        }

    }

    public void getSum() {

    System.out.println(sumweight);

    }

}

class TV implements Appliance

{

    int weight;

    public TV(int weight) {

    this.weight=weight;

    }

    @Override

    public void setWeight(int b) {

        // TODO Auto-generated method stub

        weight=b;

    }

    public int getWeight() {

    return weight;

    }

}

class WashMachine implements Appliance

{

    int weight;

    public WashMachine(int weight) {

    this.weight=weight;

        }

    @Override

    public void setWeight(int b) {

        // TODO Auto-generated method stub

        weight=b;

    }

    public int getWeight() {

    return weight;

    }

}

class AirConditioner implements Appliance

{

    int weight;

    public AirConditioner(int weight) {

    this.weight=weight;

    } 

    @Override

    public void setWeight(int b) {

        // TODO Auto-generated method stub

        weight=b;

    }

    public int getWeight() {

    return weight;

    }

}

###########################################

150 - 教师类

Time Limit: 1000  Memory Limit: 65535

Submit: 133  Solved: 60

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

______________________

import java.util.*;

public class Main{

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

    int no1=scan.nextInt();

    String name1=scan.next();

    int age1=scan.nextInt();

    String seminary1=scan.next();

        Teacher t1=new Teacher(no1,name1,age1,seminary1);


        int no2=scan.nextInt();

    String name2=scan.next();

    int age2=scan.nextInt();

    String seminary2=scan.next();

        Teacher t2=new Teacher(no2,name2,age2,seminary2);


        System.out.println(t1);

        System.out.println(t2);

        System.out.println(t1.equals(t2));

        scan.close();

    }

}

class Teacher{

int no;

String name;

int age;

String seminary;

public Teacher(int no,String name,int age,String seminary) {

this.no=no;

this.name=name;

this.age=age;

this.seminary=seminary;

}

int getNo() {return no;}

String getName() {return name;}

int getAge() {return age;}

String getSeminary() {return seminary;}

void setNo(int n) {no=n;}

void setName(String na) {name=na;}

void setAge(int a) {age=a;}

void setSeminary(String s) {seminary=s;}

public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof Teacher) {

      Teacher rec = (Teacher) o;

      if (this.no == rec.no) {

            result = true;

      }

              }

            return result;

    }

}

public String toString() {

      return "no: "+no+", name:"+name+", age: "+age+", seminary: "+seminary;

}

}

###############################

149 - 教师类-2

Time Limit: 1000  Memory Limit: 65535

Submit: 141  Solved: 53

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

____________________________________________

import java.lang.reflect.Array;

import java.util.*;

public class Main{

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

    int num=scan.nextInt();

    TeacherManagement tt=new TeacherManagement(num);

        Teacher[] t=new Teacher[num];


        for(int i=0;i

        int no=scan.nextInt();

        String name=scan.next();

        int age=scan.nextInt();

        String seminary=scan.next();

        t[i] = new Teacher(no,name,age,seminary);

        }

        tt.add(t);

        Arrays.sort(t);

        for(int i=0;i

        System.out.println(t[i].toString());

        }

        String namejuge=scan.next();

        tt.search(namejuge);

    int agejuge=scan.nextInt();

    tt.search(agejuge);

        scan.close();

    }

}

class Teacher implements Comparable{

int no;

String name;

int age;

String seminary;

public Teacher(int no,String name,int age,String seminary) {

this.no=no;

this.name=name;

this.age=age;

this.seminary=seminary;

}

int getNo() {return no;}

String getName() {return name;}

int getAge() {return age;}

String getSeminary() {return seminary;}

void setNo(int n) {no=n;}

void setName(String na) {name=na;}

void setAge(int a) {age=a;}

void setSeminary(String s) {seminary=s;}

/* public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof Teacher) {

      Teacher rec = (Teacher) o;

      if (this.no == rec.no) {

            result = true;

      }

              }

            return result;

    }

}*/

public String toString() {

      return "no: "+no+", name: "+name+", age: "+age+", seminary: "+seminary;

}

public int compareTo(Teacher t) {

if(this.no

return -1;

else if(this.no==t.no)

return 0;

else return 1;

}

}

class TeacherManagement{

int num;

Teacher[] t;

public TeacherManagement(int num) {

this.num=num;

this.t=new Teacher[num];

}

void add(Teacher[] t) {

this.t=t;

}

void search(int age){

        int f=0;

        System.out.println("search by age:");

        for (Teacher tt : t) {

            if (tt.age == age) {

                f=1;

                System.out.println(tt.toString());

            }

        }

        if(f==0){

            System.out.println("no such teacher");

        }

    }

    void search(String name){

        int f=0;

        System.out.println("search by name:");

        for (Teacher tt : t) {

            if (tt.name.equals(name)) {

                f=1;

                System.out.println(tt.toString());

            }

        }

        if(f==0){

            System.out.println("no such teacher");

        }

    }

}

###################################

142 - 计算机类

Time Limit: 1000  Memory Limit: 65535

Submit: 100  Solved: 49

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)

____________________________________

import java.lang.reflect.Array;

import java.util.*;

public class Main{

    public static void main(String[] args) {

            Scanner scan = new Scanner(System.in);

    String CPUmodel=scan.next();

    String frequency=scan.next();

    int cores = scan.nextInt();

            CPU cpu1= new CPU(CPUmodel,frequency,cores);


            String MainboardModel = scan.next();

            Mainboard mainboard1 = new Mainboard(MainboardModel);

            String MemoryModel = scan.next();

            int MemorySize = scan.nextInt();

            Memory memory1 = new Memory(MemoryModel,MemorySize);


            String ScreenModel = scan.next();

            int ScreenSize = scan.nextInt();

            Screen screen1 = new Screen(ScreenModel,ScreenSize);

            String HdModel = scan.next();

            int HdSize = scan.nextInt();

            HD hd1 = new HD(HdModel,HdSize);

            ALLMachine machine1=new ALLMachine(cpu1,mainboard1,memory1,screen1,hd1);


    String CPUmodel2=scan.next();

    String frequency2=scan.next();

    int cores2 = scan.nextInt();

            CPU cpu2= new CPU(CPUmodel2,frequency2,cores2);


            String MainboardModel2 = scan.next();

            Mainboard mainboard2 = new Mainboard(MainboardModel2);

            String MemoryModel2 = scan.next();

            int MemorySize2= scan.nextInt();

            Memory memory2 = new Memory(MemoryModel2,MemorySize2);


            String ScreenModel2 = scan.next();

            int ScreenSize2 = scan.nextInt();

            Screen screen2 = new Screen(ScreenModel2,ScreenSize2);

            String HdModel2 = scan.next();

            int HdSize2 = scan.nextInt();

            HD hd2 = new HD(HdModel2,HdSize2);


            ALLMachine machine2=new ALLMachine(cpu2,mainboard2,memory2,screen2,hd2);

            System.out.println(machine1.equals(machine2));

            System.out.println("Computer1:");

            System.out.print(machine1.toString());

            System.out.println("Computer2:");

            System.out.print(machine2.toString());

        scan.close();

    }

}

class Computer{

String model;

public Computer(String model) {

this.model=model;

}

void getPrice() {}

}

class CPU extends Computer{

String frequency;

int cores;

public CPU(String model,String frequency,int cores) {

super(model);

this.frequency=frequency;

this.cores=cores;

}

public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof CPU) {

      CPU rec = (CPU) o;

      if (this.model.equals(rec.model) && this.frequency.equals(rec.frequency)&&this.cores==rec.cores) {

            result = true;

      }

              }

            return result;

    }

}

public String toString() {

return "CPU:"+'\n'+"Model: "+model+'\n'+"Frequency: "+frequency+'\n'+"Number of Cores: "+cores+'\n';

}

}

class Mainboard extends Computer{

public Mainboard(String model) {

super(model);

}

public String toString() {

return "Mainboard:"+'\n'+"Model: "+model+'\n';

}

public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof Mainboard) {

Mainboard rec = (Mainboard) o;

      if (this.model.equals(rec.model)) {

            result = true;

      }

              }

            return result;

    }

}

}

class Memory extends Computer{

int size;

public Memory(String model,int size) {

super(model);

this.size=size;

}

public String toString() {

return "Memory:"+'\n'+"Model: "+model+'\n'+"Size: "+size+'\n';

}

public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof Memory) {

Memory rec = (Memory) o;

      if (this.model.equals(rec.model)&&this.size==rec.size) {

            result = true;

      }

              }

            return result;

    }

}

}

class Screen extends Computer{

int size;

public Screen(String model,int size) {

super(model);

this.size=size;

}

public String toString() {

return "Screen:"+'\n'+"Model: "+model+'\n'+"Size: "+size+'\n';

}

public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof Screen) {

Screen rec = (Screen) o;

      if (this.model.equals(rec.model)&&this.size==rec.size) {

            result = true;

      }

              }

            return result;

    }

}

}

class HD extends Computer{

int size;

public HD(String model,int size) {

super(model);

this.size=size;

}

public String toString() {

return "Harddisk:"+'\n'+"Model: "+model+'\n'+"Size: "+size+'\n';

}

public boolean equals(Object o) {

    if (o == null) return false;

    else {

boolean result = false;

if (o instanceof HD) {

HD rec = (HD) o;

      if (this.model.equals(rec.model)&&this.size==rec.size) {

            result = true;

      }

              }

            return result;

    }

}

}

class ALLMachine{

CPU cpu;

    Mainboard mainboard;

    Memory memory;

    Screen screen;

    HD hd;

    ALLMachine(CPU cpu,Mainboard mainboard,Memory memory,Screen screen,HD hd){

        this.cpu = cpu;

        this.mainboard = mainboard;

        this.memory = memory;

        this.screen = screen;

        this.hd= hd;

    }

    public boolean equals(Object o){

        if(o==null) return false;

        else if(o instanceof ALLMachine){

        ALLMachine c = (ALLMachine) o;

            return this.cpu.equals(c.cpu) && this.mainboard.equals(c.mainboard) && this.memory.equals(c.memory) && this.screen.equals(c.screen) && this.hd.equals(c.hd);

        }

        else

            return false;

    }

    public String toString() {

        return cpu.toString() + mainboard.toString() + memory.toString() + screen.toString() + hd.toString();

    }

}

#############################################

139 - 整数数组比较

Time Limit: 1000  Memory Limit: 65535

Submit: 82  Solved: 57

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排序

______________________________

import java.lang.reflect.Array;

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];

        for(int i=0;i

        A[i]=scan.nextInt();

        }

        int[] B=A.clone();

        Arrays.sort(B);

        int x=0,y=0,z=0;

        for(int i=0;i

        if(A[i]>B[i]) {

        x++;

        }

        else if(A[i]==B[i]) {

        y++;

        }

        else z++;

        }

        System.out.println(x);

        System.out.println(y);

        System.out.println(z);

        scan.close();

    }

}

####################################

151 - 矩阵类

Time Limit: 1000  Memory Limit: 65535

Submit: 109  Solved: 44

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

aftesr transpose:

1 4 7

2 5 8

3 8 9

_________________________

import java.lang.reflect.Array;

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();


        Matrix x=new Matrix(row,col);

        for(int i=0;i

        for(int j=0;j

        double value=scan.nextDouble();

        x.set(i,j,value);

        }

        } 

        System.out.println("row:"+row+" column:"+col);       

        int whererow=scan.nextInt();

        int wherecol=scan.nextInt();

        double wherenumber=scan.nextDouble();

        x.set(whererow-1, wherecol-1, wherenumber);

        int searchrow=scan.nextInt();

        int searchcol=scan.nextInt();

        System.out.println("after set value:");

        System.out.println(x);

        System.out.println("value on ("+searchrow+","+searchcol+"):"+(int)x.get(searchrow-1, searchcol-1));


        int addrow=scan.nextInt();

        int addcol=scan.nextInt();

        Matrix y=new Matrix(addrow,addcol);

        for(int i=0;i

        for(int j=0;j

        double value=scan.nextDouble();

        y.set(i,j,value);

        }

        }

        System.out.println(y.add(x));


        int multirow=scan.nextInt();

        int multicol=scan.nextInt();

        Matrix z=new Matrix(multirow,multicol);

        for(int i=0;i

        for(int j=0;j

        double value=scan.nextDouble();

        z.set(i,j,value);

        }

        }

        System.out.println(x.multiply(z));

        System.out.println(x.transpose());


        scan.close();

    }

}

class Matrix{

int row;

int col;

double matrix[][];

public Matrix(int row,int col) {

this.row=row;

this.col=col;

this.matrix=new double[row][col];

}

void set(int row, int col, double value){

matrix[row][col]=value;

}

double get(int row,int col) {

return this.matrix[row][col];

}

int width() {

return col;

}

int height() {

return row;

}

Matrix add(Matrix b){

System.out.println("after add:");

for(int i=0;i

for(int j=0;j

this.matrix[i][j]+=b.matrix[i][j];

}

}

return this;

}

Matrix multiply(Matrix b){

        System.out.println("after multiply:");

Matrix c=new Matrix(this.row,b.col);

for(int i=0;i

for(int j=0;j

double t=0;

for(int k=0;k

t+=this.matrix[i][k]*b.matrix[k][j];

}

c.set(i, j, t);

}

}

return c;

}

Matrix transpose() {

        System.out.println("after transpose:");

    for(int i=0;i

    for(int j=0;j

    double t=this.matrix[i][j];

    this.matrix[i][j]=this.matrix[j][i];

    this.matrix[j][i]=t;

    }

    }

    return this;

}

public String toString() {

String t="";

for(int i=0;i

for(int j=0;j

if(j!=0) t+=" ";

t+=(int)(this.matrix[i][j]);

}

if(i!=row-1)

t+="\n";

}

return t;

}

}

####################################################

你可能感兴趣的:(Java第四次作业)