Java第四次作業(yè)

140 - 家電類

Time Limit: 1000? Memory Limit: 65535

Submit: 105? Solved: 69

Description

某大型家電企業(yè)擁有一批送貨卡車,運(yùn)送電視機(jī)、洗衣機(jī)、空調(diào)等家電。編程計(jì)算每個卡車所裝載貨物的總重量。要求有一個Appliance(家電)接口和有三個實(shí)現(xiàn)類TV、WashMachine和AirConditioner,這些類能夠提供自重。有一個Truck類,包含了該貨車上的所有家電,用一個集合(數(shù)組或集合類)表示。

Main函數(shù)中程序能夠輸出Truck類所裝載貨物的總重量。

Input

家電數(shù)量

家電種類編號 家電重量

注意:各個家電的編號為: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<num;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

設(shè)計(jì)一個教師類Teacher,要求:

屬性有編號(int no)、姓名(String name)、年齡(int age)、所屬學(xué)院(String seminary),為這些屬性設(shè)置相應(yīng)的get和set方法。

為Teacher類重寫equals方法,要求:當(dāng)兩個教師對象的no相同時返回true。

重寫Teacher類的toString方法,通過該方法可以返回“no: **, name:**, age: **, seminary: **”形式的字符串。

Input

兩個教師對象的編號,姓名,年齡,學(xué)院

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對象所形成的數(shù)組可以排序(編號由低到高排序),并在main函數(shù)中使用Arrays.sort(Object[] a)方法排序

2. 定義一個類TeacherManagement,包含教師數(shù)組,提供方法add(Teacher[]),使其可以添加教師,提供重載方法search,方法可以在一組給定的教師中,根據(jù)姓名或年齡返回等于指定姓名或年齡的教師的字符串信息,信息格式為:“no: **, name:**, age: **, seminary: **”。如果沒有滿足條件的教師,則返回“no such teacher”。

Input

教師個數(shù)

教師信息

待查找教師的姓名

待查找教師的年齡

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

}

public int compareTo(Teacher t) {

if(this.no<t.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 - 計(jì)算機(jī)類

Time Limit: 1000? Memory Limit: 65535

Submit: 100? Solved: 49

Description

構(gòu)造計(jì)算機(jī)類,其中包含其配置信息:處理器、主板、內(nèi)存、顯示器、硬盤等設(shè)備,各個設(shè)備均有型號(字符串),特別的,處理器有主頻(小數(shù))和內(nèi)核數(shù)(整數(shù))、顯示器有尺寸(整型)、內(nèi)存和硬盤有容量數(shù)據(jù)(GB為單位)。請你嘗試構(gòu)造合適的類和類的關(guān)系來表示計(jì)算機(jī),并為該計(jì)算機(jī)類添加計(jì)算價格(各設(shè)備價格之和)、打印配置信息等方法。重寫相關(guān)類的equals方法,使得兩個配置完全相同的計(jì)算機(jī)為相同的計(jì)算機(jī)。重寫相關(guān)類的toString函數(shù),打印計(jì)算機(jī)的配置信息。

在main函數(shù)中

Input

兩個計(jì)算機(jī)對象,包含

CPU:型號、主頻、內(nèi)核

主板:型號

內(nèi)存:容量

顯示器:尺寸

硬盤:容量

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

為計(jì)算機(jī)類和各個組成配件都構(gòu)造類,分別重寫toString和equals方法

在計(jì)算機(jī)類中調(diào)用各個配件的equals和toString方法

回車用\n

小數(shù)用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 - 整數(shù)數(shù)組比較

Time Limit: 1000? Memory Limit: 65535

Submit: 82? Solved: 57

Description

給定兩個整型數(shù)組A和B,將A的元素復(fù)制到B中,使得兩個數(shù)組完全相同。再將B數(shù)組從小到大排列,將兩數(shù)組的同一位置上對應(yīng)的元素進(jìn)行比較,統(tǒng)計(jì)出A中大于B的元素個數(shù),等于B中元素的個數(shù),小于B中的元素的個數(shù)。

Input

數(shù)組A的個數(shù)

數(shù)組A元素

Output

A大于B的個數(shù)

A等于B的個數(shù)

A小于B的個數(shù)

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<num;i++) {

? ? ? ? A[i]=scan.nextInt();

? ? ? ? }

? ? ? ? int[] B=A.clone();

? ? ? ? Arrays.sort(B);

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

? ? ? ? for(int i=0;i<num;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

利用二維數(shù)組(double[])實(shí)現(xiàn)一個矩陣類:Matrix。要求提供以下方法:(1)set(int row, int col, double value):將第row行第col列的元素賦值為value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩陣的列數(shù);(4)height():返回矩陣的行數(shù);(5)Matrix add(Matrix b):返回當(dāng)前矩陣與矩陣b相加后的矩陣;(6)Matrix multiply(Matrix b):返回當(dāng)前矩陣與矩陣b相乘后的矩陣。(7)Matrix transpose():返回當(dāng)前矩陣的轉(zhuǎn)置矩陣;(8)toString():以行和列的形式打印出當(dāng)前矩陣。

Input

矩陣的行列數(shù)

矩陣的數(shù)據(jù)

設(shè)置矩陣值的行、列和值

獲取矩陣值的行、列

待相加矩陣的行列數(shù)

待相加矩陣的值

待相乘矩陣的行列數(shù)

待相乘矩陣的值

Output

矩陣的行、列數(shù)

設(shè)置矩陣值后的矩陣

某行某列的矩陣值

矩陣相加結(jié)果

矩陣相乘結(jié)果

矩陣轉(zhuǎn)置結(jié)果

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<row;i++) {

? ? ? ? for(int j=0;j<col;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<addrow;i++) {

? ? ? ? for(int j=0;j<addcol;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<multirow;i++) {

? ? ? ? for(int j=0;j<multicol;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<b.row;i++) {

for(int j=0;j<b.col;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<this.row;i++) {

for(int j=0;j<b.col;j++) {

double t=0;

for(int k=0;k<this.col;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<this.row;i++) {

? ? for(int j=0;j<i;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<this.row;i++) {

for(int j=0;j<this.col;j++) {

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

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

}

if(i!=row-1)

t+="\n";

}

return t;

}

}

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,727評論 18 399
  • 50道經(jīng)典Java編程練習(xí)題,將數(shù)學(xué)思維運(yùn)用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會聲哈~ 1.指數(shù)...
    OSET我要編程閱讀 7,289評論 0 9
  • 【程序1】 題目:古典問題:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔...
    開心的鑼鼓閱讀 3,394評論 0 9
  • 不要喪,你會覺得原來喪的時間都是在浪費(fèi)無意義,如果喪的你知道后來會有這樣一天,你一定不會難過而是開開心心,因?yàn)槲磥?..
    theunicole閱讀 265評論 0 0
  • 以前跟朋友討論各自前任 她說像她前任那樣 詛咒他一輩子找不到對象 我說我感覺我還是希望前任好 也沒有怨恨 雖然不在...
    清風(fēng)過南巷閱讀 219評論 0 0

友情鏈接更多精彩內(nèi)容