java程序设计精讲上机实践答案(最好是第3章和4,5章的答案)

java程序设计精讲上机实践答案(最好是第3章和4,5章的答案),第1张

第三章

3import javautilScanner;

public class TestScanner3{

public static void main(String[] args){

Scanner s=new Scanner(Systemin);

Systemoutprint("请输入三角形的三条边:");

float a=snextFloat();

float b=snextFloat();

float c=snextFloat();

if((a!=0)&&(b!=0)&&(c!=0)&&(a+b>c&&Mathabs(a-b)<c))

Systemoutprintln("\t三条边构成三角形 ");

else

Systemoutprintln("\t三条边构不成三角形 ");

}

}

4import javautilScanner;

public class TestSwitch4{

public static void main(String args[]){

Scanner s=new Scanner(Systemin);

Systemoutprint("请输入两个整数:");

int a=snextInt();

int b=snextInt();

char c;

switch(1){

case 1: Systemoutprintln(" "+"="+a+"+"+b);

case 2: Systemoutprintln(" "+"="+a+"-"+b);

case 3: Systemoutprintln(" "+"="+a+""+b);

case 4: Systemoutprintln(" "+"="+a+"/"+b); break;

default: Systemoutprintln("输入错误: ");

}

}

}

5public class Sum5{

public static void main(String args[]){

int s=0,s1=0,s2=0;

for(int i=1;i<=3;i++)

{

if(i/2==0)

s1+=(-1)ii;

else

s2+=ii;

}

s=s1+s2;

Systemoutprintln("s的值是:"+s);

}

}

6public class Sum6{

public static void main(String args[]){

long t=0;

for(int i=1;i<=20;i++)

{ long s=1;

for(int j=1;j<=i;j++)

{

s=sj;

}

Systemoutprintln(i+"!的值是: \n"+s);

t=t+s;

}

Systemoutprintln("t的值是: "+t);

}

}

第四章

1public class Box{

public int length;

public int width;

public int height;

public Box(int length1,int width1,int height1)

{ length=length1;width=width1;height=height1;}

public int Volume()

{ int v;

v=lengthwidthheight;

return v;

}

public int Som()

{ int s;

s=2lengthwidth+2widthheight+2lengthheight;

return s;

}

public static void main(String[] args)

{ Box b;

b=new Box(20,10,5);

Systemoutprintln("长方体的长: "+blength);

Systemoutprintln("长方体的宽: "+bwidth);

Systemoutprintln("长方体的高: "+bheight);

Systemoutprintln("长方体的体积: "+ bVolume());

Systemoutprintln("长方体的表面积: "+ bSom());

}

}

2public class A {

private int data;

private String str;

public A(){

data = 0;

str = "";

}

public A(int data,String str){

thisdata = data;

thisstr = str;

}

public void add(int k,String s){

thisdata+=k;

thisstr+=s;

}

public void clear(){

thisdata = 0;

thisstr = "";

}

public String toString(){

String s = data+"";

s+=str;

return (s);

}

public static void main(String args[]){

A a = new A();

aadd(10,"Hello");

Systemoutprintln("data is:"+adata+" str is:"+astr);

Systemoutprintln(atoString());

aclear();

A b = new A(199,"nihao");

Systemoutprintln("data is:"+bdata+" str is:"+bstr);

Systemoutprintln(btoString());

badd(200, " dajiahao");Systemoutprintln("data is:"+bdata+" str is:"+bstr);

Systemoutprintln(btoString());

}

}

没有第五章

对几个容易错的稍微注解下,

1 D

2 C final修饰

3 B

4 B

5 C

6 B

7 C

8 C

9 B JAVA里面都是写作null的,而不是NULL

10 A: Stringsubstring()大小写问题

11 C

12C

13A

14C

15A

16B

17C

18C

19D

20D管道流,此流一般用于多线

public class Car1 extends Car {

public String manufacturer;

public Car1(String color, String style, String manufacturer) {

super(color, style);

thismanufacturer = manufacturer;

}

public Car1(String color) {

thiscolor = color;

thismanufacturer = "上海大众";

}

@Override

public String toString() {

return "Car1 [manufacturer=" + manufacturer + ", color=" + color + ", style=" + style + "]";

}

public Car1() {

super();

}

public static void main(String[] args) {

Car1 a = new Car1();

Car1 b = new Car1("黑色", "卡车", "斯泰尔");

Car1 c = new Car1("白色");

Systemoutprintln(atoString());

Systemoutprintln(btoString());

Systemoutprintln(ctoString());

}

}

class Car {

public String color;

public String style;

public Car() {

thiscolor = "red";

thisstyle = "car";

}

public Car(String color, String style) {

thiscolor = color;

thisstyle = style;

}

public void showColor() {

Systemoutprintln("颜色   " + thiscolor);

}

public void showStyle() {

Systemoutprintln("型号   " + thisstyle);

}

}

importjavaio;

importjavaioFile;

importjavaioFileReader;

importjavaioFileWriter;

importjavaioIOException;

publicclassNewTxt{

publicstaticvoidmain(String[]args)throwsIOException{

intn=5;//NN数组

double[][]arr=newdouble[n][n];//插入的数组

double[][]arr2=newdouble[n][n];;//读取出的数组

//数组初始化,随机生成的[0,100)之间的double数

for(inti=0;i

for(intj=0;j

arr[i][j]=Mathrandom()100;

Systemoutprintln(arr[i][j]);

}

}

Filefile=newFile("d:\arraytxt");//存放数组数据的文件

FileWriterout=newFileWriter(file);//文件写入流

//将数组中的数据写入到文件中。每行各数据之间TAB间隔

for(inti=0;i

for(intj=0;j

outwrite(arr[i][j]" ");

}

outwrite("");

}

outclose();

in=new(newFileReader(file));//

Stringline;//一行数据

introw=0;

//逐行读取,并将每个数组放入到数组中

while((line=inreadLine())!=null){

String[]temp=linesplit(" ");

for(intj=0;j

arr2[row][j]=DoubleparseDouble(temp[j]);

}

row;

}

inclose();

//显示读取出的数组

for(inti=0;i

for(intj=0;j

Systemoutprint(arr2[i][j]" ");

}

Systemoutprintln();

}

}

}

(1)。public class TiaoSeBan extends JFrame {

JPanel panel1;

JPanel toppanel;

JPanel bottompanel;

JPanel colorLabpanel;

JPanel colorScrollBarpanel;

JLabel redLable;

JLabel greenLable;

JLabel blueLable;

JLabel showColorLable;

JScrollBar redScrollBar;

JScrollBar greenScrollBar;

JScrollBar blueScrollBar;

void init(){

panel1=new JPanel();

toppanel=new JPanel();

bottompanel=new JPanel();

colorLabpanel=new JPanel();

colorScrollBarpanel=new JPanel();

redLable=new JLabel("Red");

greenLable =new JLabel("Green");

blueLable=new JLabel("Blue");

showColorLable=new JLabel("Show Colors");

redScrollBar =new JScrollBar(JScrollBarHORIZONTAL , 0, 100,0,255);

greenScrollBar =new JScrollBar(JScrollBarHORIZONTAL , 0,100,0,255);

blueScrollBar =new JScrollBar(JScrollBarHORIZONTAL , 0, 100,0,255);

}

TiaoSeBan(){

super();

init();

setLayout(new BorderLayout());

add(toppanel,BorderLayoutCENTER);

add(bottompanel,BorderLayoutSOUTH);

showColorLablesetHorizontalAlignment(SwingConstantsCENTER);

toppanelsetLayout(new BorderLayout());

toppaneladd(showColorLable,BorderLayoutCENTER);

bottompanelsetLayout(new BorderLayout());

bottompaneladd(colorLabpanel,BorderLayoutWEST);

bottompaneladd(colorScrollBarpanel,BorderLayoutCENTER);

colorLabpanelsetLayout(new GridLayout(3, 1));

colorLabpaneladd(redLable);

colorLabpaneladd(greenLable);

colorLabpaneladd(blueLable);

colorScrollBarpanelsetLayout(new GridLayout(3, 1));

colorScrollBarpaneladd(redScrollBar);

colorScrollBarpaneladd(greenScrollBar);

colorScrollBarpaneladd(blueScrollBar);

redScrollBaraddAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable);

}

});

greenScrollBaraddAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable);

}

});

blueScrollBaraddAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

reSetColor(showColorLable);

}

});

}

void reSetColor(JLabel label){

labelsetForeground(new Color(redScrollBargetValue(), greenScrollBargetValue(), blueScrollBargetValue()));

}

public static void main(String[] args) {

TiaoSeBan frame=new TiaoSeBan();

framesetTitle("tiaoseban");

framesetLocationRelativeTo(null);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetSize(200,200);

framesetVisible(true);

}

}

(2)public class jisuanq extends JApplet implements ActionListener {

private JTextField jtf = new JTextField(10);

private boolean newNumber = true;

private int result = 0;

private String op = "=";

public void init() {

JPanel p = new JPanel();

psetLayout(new BorderLayout());

JPanel westPanel = new JPanel();

westPanelsetLayout(new GridLayout(5, 0));

westPaneladd(new JButton(" "));

westPaneladd(new JButton("MC"));

westPaneladd(new JButton("MR"));

westPaneladd(new JButton("MS"));

westPaneladd(new JButton("M+"));

Panel centerPanel = new Panel();

centerPanelsetLayout(new BorderLayout());

Panel p1 = new Panel();

Panel p2 = new Panel();

p1setLayout(new FlowLayout(FlowLayoutRIGHT));

p1add(new JButton("Back"));

p1add(new JButton("CE"));

p1add(new JButton("C"));

p2setLayout(new GridLayout(4, 5));

JButton bt;

p2add(bt = new JButton("7"));

btaddActionListener(this);

p2add(bt = new JButton("8"));

btaddActionListener(this);

p2add(bt = new JButton("9"));

btaddActionListener(this);

p2add(bt = new JButton("/"));

btaddActionListener(this);

p2add(bt = new JButton("sqrt"));

btaddActionListener(this);

p2add(bt = new JButton("4"));

btaddActionListener(this);

p2add(bt = new JButton("5"));

btaddActionListener(this);

p2add(bt = new JButton("6"));

btaddActionListener(this);

p2add(bt = new JButton(""));

btaddActionListener(this);

p2add(bt = new JButton("%"));

btaddActionListener(this);

p2add(bt = new JButton("1"));

btaddActionListener(this);

p2add(bt = new JButton("2"));

btaddActionListener(this);

p2add(bt = new JButton("3"));

btaddActionListener(this);

p2add(bt = new JButton("-"));

btaddActionListener(this);

p2add(bt = new JButton("1/x"));

btaddActionListener(this);

p2add(bt = new JButton("0"));

btaddActionListener(this);

p2add(bt = new JButton("+/-"));

btaddActionListener(this);

p2add(bt = new JButton(""));

p2add(bt = new JButton("+"));

btaddActionListener(this);

p2add(bt = new JButton("="));

btaddActionListener(this);

centerPaneladd(p2, BorderLayoutCENTER);

centerPaneladd(p1, BorderLayoutNORTH);

padd(centerPanel, BorderLayoutCENTER);

padd(westPanel, BorderLayoutWEST);

getContentPane()setLayout(new BorderLayout());

getContentPane()add(p, BorderLayoutCENTER);

getContentPane()add(jtf, BorderLayoutNORTH);

}

public void actionPerformed(ActionEvent e) {

String actionCommand = egetActionCommand();

if ('0' <= actionCommandcharAt(0) &&

actionCommandcharAt(0) <= '9') {

if (newNumber) {

jtfsetText(actionCommand);

newNumber = false;

}

else {

jtfsetText(jtfgetText() + actionCommand);

}

}

else

if (newNumber) {

if (actionCommandequals("-")) {

jtfsetText("-");

newNumber = false;

}

else

op = actionCommand;

}

else {

execute();

op = actionCommand;

}

}

void execute() {

int number = new Integer(jtfgetText())intValue();

Systemoutprintln("number " + op);

switch (opcharAt(0)) {

case '+': result += number; break;

case '-': result -= number; break;

case '': result = number; break;

case '/': result /= number; break;

case '%': result %= number; break;

case '=': result = number;

}

Systemoutprintln("result "+result);

jtfsetText(new Integer(result)toString());

newNumber = true;

}

/This main method enables the applet to run as an application/

public static void main(String[] args) {

// Create a frame

JFrame frame = new JFrame("Exercise16_8");

// Create an instance of the applet

jisuanq applet = new jisuanq();

// Add the applet instance to the frame

framegetContentPane()add(applet, BorderLayoutCENTER);

// Invoke init() and start()

appletinit();

appletstart();

// Display the frame

framesetSize(300, 300);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetLocationRelativeTo(null);

framesetVisible(true);

}

}

以上就是关于java程序设计精讲上机实践答案(最好是第3章和4,5章的答案)全部的内容,包括:java程序设计精讲上机实践答案(最好是第3章和4,5章的答案)、《JAVA程序设计》考题急求答案,急求java试题简答,java课后习题答案.(拒绝打酱油)、java程序设计,求答案等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zz/10048274.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存