java语言程序设计课后答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java语言程序设计课后答案
作业参考答案
习题一
4、如何建立和运行Java程序,
首先启动文本编辑器,如记事本、UltraEdit等,编辑程序代码,并以.Java
作为文件扩展名保存程序源代码;然后进入dos环境利用javac编译源程序,生成扩展名为.class的字节码文件;再利用命令java运行字节码文件,得到程序的运行结果。

在集成开发环境Jbuilder、Eclipse下,可以完成程序的编辑、编译、调试及运行等所有任务。

5、
public class LikeJava
{
public static void main(String [] args)
{
System.out.println(“I Like Java Very much!”);
}
}
习题二
5、
(1) 45 (2) false (3) 14 (4) 14 (5),6 (6) true
(7) 12
9、
public class Volume
{
public static void main(String [] args) {
double r=0,v=0;
r=double.parseDouble(args[0]);
v=4*3.14159/3*r*r*r;
System.out.println(“球体积为:”+v);
}
}
习题三
8、
public class Factorials {
public static void main(String args[]) {
int i, j;
long s=0, k;
i=1;
do //外循环开始
{
k = 1;
j=1;
do{//内循环开始
k = k * j; //内循环体
j++;
}while(j<=i);//内循环结束
System.out.println(i + "!=" + k);
s = s + k;
i++;
}while(i<=20); //外循环结束
System.out.println("Total sum=" + s); }
}
10、
public class Num
{
public static void main(String[]args) {
int i,j,k,n;
for (n=100;n<1000;n++)
{
i=n/100;
j=(n-i*100)/10;
k=n%10;
if (i*i*i+j*j*j+k*k*k==n)
System.out.print(n+" ");
}
}
}
习题四
5、
import java.util.Scanner;
class Factor{
long fac(int m)
{if(m==0||m==1)return 1;
else return m*fac(m-1);
}
public static void main(String [] args)
{int i,n;
long sum=0;
String s="";
Scanner input=new Scanner(System.in);
System.out.print("Please input n: ");
n=input.nextInt();
Factor f=new Factor();
for(i=1;i<=n;i++)
{ System.out.println(f.fac(i));
sum=sum+f.fac(i);
s=s+i+"!+";
}
System.out.println(s.substring(0,s.length()-1)+"="+sum); }
}
习题五
2、
import java.io.*;
public class YangHuiOk
{
public static void main (String args[]) throws IOException {
int max,a[][],i,j;
char x;
System.out.print("请输入杨辉三角要显示的行数: ");
x=(char)System.in.read();
max = Integer.parseInt(String.valueOf(x));
a=new int[max][];
for (i=0;i<max;i++)
{
a[i]=new int[i+1];
}
a[0][0]=1;
for (i=1;i<max;i++)
{
a[i][0]=1;
a[i][a[i].length-1]=1;
for (j=1;j<a[i].length-1;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(i=0;i<max;i++)
{
//for(j=0;j<=max-i;j++) System.out.print(" ");
for(j=0;j<=a[i].length-1;j++) System.out.print(a[i][j]+" "); System.out.println();
}
}
}
5、
import java.util.Scanner;
public class MatrixTurn {
public static void main (String[] args) {
int m,n;
Scanner input=new Scanner(System.in);
System.out.print("请输入矩阵的行数: ");
m=input.nextInt();
System.out.print("请输入矩阵的列数: ");
n=input.nextInt();
Matrix t=new Matrix(m,n);
for(int i=1;i<=m;i++)//为矩阵各元素赋值
for (int j=1;j<=n;j++)
t.setElement(Math.random(),i,j);
System.out.println("转置前的矩阵如下: ");
for(int i=1;i<=m;i++)
{for (int j=1;j<=n;j++)
//System.out.print(t.matrix[i][j]+" ");
System.out.print(t.getElement(i,j)+" ");//访问矩阵元素方法1 System.out.println();}
Matrix z;//声明转置矩阵
z=t.turn(t);
System.out.println("转置后的矩阵如下: ");
for(int i=0;i<n;i++)
{for (int j=0;j<m;j++)
System.out.print(z.matrix[i][j]+" ");//访问矩阵元素方法2,前提是matrix
前无private
System.out.println();}
}
}
习题六
9、
public class Vehicle
,
String color, kind;
int speed;
Vehicle()
{
color=”Red”;
kind=”卡车”;
speed=0;
}
public void setColor(String color1) { color=color1;
}
public void setSpeed(String speed1) { speed=speed1;
}
public void setKind(String kind1) { kind=kind1;
}
public String getColor( ) {
return color;
}
public String getKind( ) {
return kind;
}
public int getSpeed( ) {
return speed;
}
public static void main(String [] args)
{
Vehicle che=new Vehicle ();
Che.setColor(“Blue”);
Che.setSpeed(150);
Che.setKind(“跑车”);
System.out.p rintln(“有一辆”+che.getColor()+”的”+che.getKind()+”行驶在高速公
路上”);
System.out.println(“时速”+che.getSpeed()+”km/h”); }
}
习题七 7、
public class Vehicle ,
String color, kind;
int speed;
Vehicle()
{
color=” ”;
kind=” ”;
speed=0;
}
public void setColor(String color1)
{
color=color1;
}
public void setSpeed(String speed1) {
speed=speed1;
}
public void setKind(String kind1) {
kind=kind1;
}
public String getColor( ) {
return color;
}
public String getKind( ) {
return kind;
}
public int getSpeed( ) {
return speed;
}
}
public class Car extends Vehicle {
int passenger;
public Car()
{
super();
passenger=0;
}
public void setPassenger(int passenger)
{
this. passenger = passenger; }
public int getPassenger( ) {
return passenger;
}
public static void main(String [] args)
{
Car benz=new Car();
benz.setColor(“Yellow”);
benz.setKind(“roadster”);
benz.setSpeed(120);
benz.setPassenger(4);
System.out.println(“benz: “);
System.out.println(“Color “+benz.getColor());
System.out.print(“Speed (km/h)“);
System.out.println(benz.getSpeed()); System.out.println(“Kind: “+benz.getKind()); System.out.print(“Passenger: “);
System.out.println(benz.getPassenger());
}
}
习题九
4、
import java.io.*;
public class UseException
{
public static void main(String [] args)
{
System.out.println("请输入一个整数字符串");
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int a=Integer.parseInt(in.readLine());
System.out.println("您输入的整数是:"+a);
}
catch(IOException e)
{
System.out.println("IO错误");
}
catch(NumberFormatException e1)
{
System.out.println("您输入的不是一个整数字符串");
}
}
}
习题十 7、
import java.io.*;
public class SaveName {
public static void main(String [] args)
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new FileWriter("name.txt"));
String s;
while(true)
{
System.out.println("请输入姓名:");
s=br.readLine();
if(s.length()==0)
break;
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
}
catch(FileNotFoundException e)
{
System.out.println(e.toString());
}
catch(IOException e1)
{
System.out.println(e1.toString());
}
}
}
8、
import java.io.*;
public class SaveGrade
{
public static void main(String [] args)
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new FileWriter("grade.txt"));
String s,ss;
while(true)
{
System.out.println("请输入姓名:");
s=br.readLine();
if(s.length()==0)
break;
bw.write(s);
bw.newLine();
System.out.println("请输入学号:");
s=br.readLine();
bw.write(s);
bw.newLine();
System.out.println("请输入成绩:");
s=br.readLine();
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
int max=0,min=100,total=0,num=0;
BufferedReader bf=new BufferedReader(new FileReader("grade.txt")); while(true)
{
ss=bf.readLine();
if(ss==null)
break;
ss=bf.readLine();
ss=bf.readLine();
int grade=Integer.parseInt(ss);
total+=grade;
num+=1;
if(grade>max)max=grade;
if(grade<min)min=grade;
}
System.out.println("学生成绩中最高为:"+max+",最低为:"+min+",平均分为:"+total*1.0/num);
bf.close();
}
catch(FileNotFoundException e)
{
System.out.println(e.toString());
}
catch(IOException e1)
{
System.out.println(e1.toString());
}
}
}
习题十一
6、
import java.awt.*;
import java.awt.event.*;
public class ChangeColor extends Frame { private Button red=new Button("红");
private Button green=new Button("绿"); private Button blue=new Button("蓝"); private TextField text=new TextField(); public ChangeColor()
{
super("改变颜色");
this.setLayout(null);
text.setBackground(Color.WHITE);
red.setBounds(25,50,50,20);
this.add(red);
green.setBounds(125,50,50,20);
this.add(green);
blue.setBounds(225,50,50,20);
this.add(blue);
text.setBounds(25,100,250,30);
this.add(text);
red.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
text.setBackground(Color.RED);
}
});
green.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
text.setBackground(Color.GREEN);
}
});
blue.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
text.setBackground(Color.BLUE);
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(300,200);
setVisible(true);
}
public static void main (String[] args)
{
ChangeColor color=new ChangeColor(); }
}
习题十二
5、
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Goods extends JFrame {
private JComboBox list;
private JTextArea info;
private String names[]={
"请选择你要查询的商品",
"A商品",
"B商品",
"C商品",
"D商品",
"E商品",
"F商品"
};
private String goods[][]={ {"","",""},
{"A商品","北京",",300"},
{"B商品","上海",",400"},
{"C商品","广州",",500"},
{"D商品","长沙",",600"},
{"E商品","武汉",",700"},
{"F商品","天津",",800"}
};
public Goods()
{
super("商品信息");
Container pane=this.getContentPane();
pane.setLayout(new BorderLayout());
list=new JComboBox(names);
info=new JTextArea(5,20);
pane.add(list,BorderLayout.NORTH);
pane.add(info,BorderLayout.CENTER);
list.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) {
int index=list.getSelectedIndex();
info.setText("商品名:"+goods[index][0]+"\n"); info.append("产地:"+goods[index][1]+"\n"); info.append("价格:"+goods[index][2]+"\n"); }
});
this.setSize(250,300);
this.setVisible(true);
}
public static void main (String[] args) {
Goods ccc=new Goods();
ccc.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}。

相关文档
最新文档