第1个回答 推荐于2018-03-12
邮件已发送,请查收。
1.:
public class Matrix {
int i[][];
Matrix(){//构造一个10X10个元素的矩阵,没有数据
i=new int[10][10];
}
Matrix(int n,int m){//构造一个n*m个元素的矩阵,数据由随机数产生
i=new int[n][m];
for(int j=0;j<=n-1;j++){
for(int k=0;k<=m-1;k++)
i[j][k]=(int) (Math.random()*10);
}
}
Matrix(int table[][]){//以一个整型的二维数组构造一个矩阵
i=table;
}
public void output(){//输出Matrix类中数组的元素值
for(int j=0;j<=i.length-1;j++){
for(int k=0;k<=i[0].length-1;k++)
System.out.print(i[j][k]+" ");
System.out.println();
}
}
public void transpose(){//输出一个矩阵的转置矩阵
for(int j=0;j<=i[0].length-1;j++){
for(int k=0;k<=i.length-1;k++)
System.out.print(i[k][j]+" ");
System.out.println();
}
}
public static void main(String args[]){
Matrix m=new Matrix(4,5);
m.output();
System.out.println();
m.transpose();
Square s=new Square(5);
s.output();
System.out.println();
s.transpose();
}
}
public class Square extends Matrix{
Square(){
i=new int[10][10];
}
Square(int b){
super(b,b);
}
Square(int table[][]){
if(table.length==table[0].length)
i=table;
}
}
2:
public class Complex {
int x,y;
Complex(){}
Complex(int i,int j){
x=i;
y=j;
}
public void showComp(){
if(y>=0)
System.out.println(x+"+"+y+"i");
else
System.out.println(x+""+y+"i");
}
public Complex addComp(Complex C1,Complex C2){
return new Complex(C1.x+C2.x,C1.y+C2.y);
}
public Complex subComp(Complex C1,Complex C2){
return new Complex(C1.x-C2.x,C1.y-C2.y);
}
public Complex multiComp(Complex C1,Complex C2){
return new Complex(C1.x*C2.x-C1.y*C2.y,C1.x*C2.y+C2.x*C1.y);
}
public boolean equalComp(Complex C1,Complex C2){
return C1.x==C2.x&&C1.y==C2.y;
}
public static void main(String args[]){
Complex c=new Complex(1,2);
c.showComp();
c.addComp(new Complex(-1,3), new Complex(3,-3)).showComp();
c.subComp(new Complex(-1,3), new Complex(3,-3)).showComp();
c.multiComp(new Complex(-1,3), new Complex(3,-3)).showComp();
System.out.println(c.equalComp(new Complex(-1,3), new Complex(3,-3)));
System.out.println(c.equalComp(new Complex(3,-3), new Complex(3,-3)));
}
}
3.:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LayoutManager {
public static void main(String args[]){
JFrame jf=new JFrame();
JButton jbA=new JButton("Hello,");
JButton jbB=new JButton("取消");
JButton jbC=new JButton("确定");
JButton jbD=new JButton("Java!......");
JButton jb0=new JButton("0");
JButton jb1=new JButton("1");
JButton jb2=new JButton("2");
JButton jb3=new JButton("3");
JButton jb4=new JButton("4");
JButton jb5=new JButton("5");
JButton jb6=new JButton("6");
JButton jb7=new JButton("7");
JButton jb8=new JButton("8");
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(0,3));
jp.add(jb0);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jp.add(jb7);
jp.add(jb8);
jf.add(jbA,BorderLayout.NORTH);
jf.add(jbB,BorderLayout.WEST);
jf.add(jbC,BorderLayout.EAST);
jf.add(jbD,BorderLayout.SOUTH);
jf.add(jp,BorderLayout.CENTER);
jf.setTitle("布局管理器");
jf.setSize(300, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}本回答被提问者和网友采纳