1、1. 定义一个包括10个元素一维整型数组,通过从键盘输入的10个整数对数组进行初始化,将数组中的元素按小到大排序后在屏幕上显示,求出该数组中元素的最小值、最大值以及平均值并在屏幕上显示输出。 解答: import java.util.Scanner; import java.util.Arrays; public class Test{ public static void main(String[] args) { int[] x = new int[10]; double sum =0; Scanner scr = new Scanner(System.in)
2、
for(int i=0;i 3、学号(id)、姓名(name)、英语成绩(eng)、数学成绩(math)、计算机成绩(comp)和总成绩(sum)
方法包括:构造方法、各属性的set方法、各属性的get方法、toString方法(输出学生的全部信息)、sum方法(计算总成绩)。
解答:
public class Student implements Serializable {
//属性定义
public Student(String id,String name,int eng,int math,int comp){
this.id=id;
this.name=name;
this.eng= 4、eng;
this.math=math;
p=comp;
sum(); //计算总成绩 }
public Student(Student s){
this.id=s.id;
this.name=new String(s.name);
this.eng=s.eng;
this.math=s.math;
p=p;
sum(); //计算总成绩 }
public void setId(String id){ this.id=id; }
public void setName( 5、String name){
this.name=name; }
public void setEng(int eng){
this.eng=eng; sum(); }
public void setMath(int math){
this.math=math; sum(); }
public void setComp(int comp){
p=comp; sum(); }
public String getId(){ return id; }
public String getName(){ return name; }
pu 6、blic int getEng(){ return eng; }
public int getMath(){ return math; }
public int getComp(){ return comp; }
public int getSum(){ return sum; }
void sum(){ this.sum=eng+math+comp; }
public String toString(){
return getId() + "\t"+getName() + "\t"+getEng() + "\t"+getMath() +"\t"+g 7、etComp() + "\t"+getSum();} }
3. 设计一个一元二次方程类,并为这个类添加异常处理。
解答:
public class Operation {
public static void main(String[] args)
{
String s="";
double a,b,c,r1,r2;
System.out.print("求二元一次方程的根") ;
System.out.print("请键入第一个系数a") ;
try
{
BufferedReader in =new BufferedReader 8、
new InputStreamReader(System.in));
s=in.readLine();
}catch(IOException e){}
a=Double.parseDouble(s);
System.out.print("请键入第二个系数b") ;
try
{
BufferedReader in =new BufferedReader(
new InputStreamReader(System.in)); 9、
s=in.readLine();
}catch(IOException e){}
b=Double.parseDouble(s);
System.out.print("请键入第三个系数c") ;
try
{
BufferedReader in =new BufferedReader(
new InputStreamReader(System.in));
s=in.readLine();
}catch(IOEx 10、ception e){}
c=Double.parseDouble(s);
r1=(-b+Math.sqrt(b*b-4*a*c))/(2*a);
r2=(-b-Math.sqrt(b*b-4*a*c))/(2*a);
System.out.print("该二元一次方程的根为:"+r1+"和"+r2) ;
}
}
4.编写一个应用程序创建两个线程,一个线程打印输出1~1000之间所有3的倍数,另外一个线程打印输出1000~2000之间所有5的倍数。
解答:
class Thread1 extends Thread{
public Thread1(St 11、ring msg){
super(msg);
}
public void run(){
int sum=0;
for(int i=1;i<=1000;i++){
if(i % 3 ==0)
System.out.println(i);
}
}
}
class Thread2 extends Thread{
public Thread2(String msg){
super(msg);
}
public void run(){
int sum=0;
for(int i=1000;i<=2000;i 12、){
if(i % 5 ==0)
System.out.println(i);
}
}}
public class Exam5{
public static void main(String[] args){
Thread1 x = new Thread1("Thread1");
Thread2 y = new Thread2("Thread2");
x.start();
y.start();
}
}
5.水仙花数是指这样的三位数,其个位、十位和百位三个数的平方和等于这个三位数本身,请编写程序打印输出所有(100~99 13、9之间)的水仙花数。
解答:
public class Narcissus{
public static void main(String args[]){
int i,j,k,n=100,m=1;
while (n<1000){
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i,3)+Math.pow(j,3)+Math.pow(k,3))==n)
System.out.println("找到第"+m++ +"个水仙花数:"+n);
n++;
}
14、
m=1;
// 或者使用下面的方法
for (n=100;n<1000;n++){
i=n/100;
j=(n-i*100)/10;
k=n%10;
if((Math.pow(i,3)+Math.pow(j,3)+Math.pow(k,3))==n)
System.out.println("找到第"+m++ +"个水仙花数:"+n);
n++;
}
}
}
6. 编写程序随机生成10个1到200之间的正整数,打印输出这些随机数并求它们的最大值、最小值、平均值 15、
解答:
import java.util.Arrays;
public class Test{
public static void main(String[] args){
int[] a= new int[10];
double sum=0;
for(int i=0;i 16、[0]);
System.out.println("max="+a[a.length-1]);
System.out.println("average="+(sum/a.length));
}
}
7.按以下要求定义一个类Circle描述一个圆,并完成相应的操作:
(1) 实例属性:圆心x坐标(xpoint)、圆心y坐标(ypoint)和半径(radius)。
(2) 构造方法:给3个属性赋初值。
(3) 实例方法(area):求圆的面积。
(4) 实例方法(circumference):求圆的周长。
(5) 重写toString()方法,返回圆心坐标和半径。
( 17、6) 实例化这个类,调用方法完成信息的输出。
解答:
class Circle{
private double xpoint;
private double ypoint;
private double radius;
public Circle(double x,double y,double r){
xpoint = x;
ypoint = y;
radius = r;
}
public double area(){
return Math.PI*radius*radius;
}
public double circumferenc 18、e(){
return 2* Math.PI*radius;
}
public String toString(){
return "圆心:("+xpoint+","+ypoint+")"+" 半径:"+radius;
}
}
public class Test{
public static void main(String[] args) {
Circle obj = new Circle(0,0,100);
System.out.println(obj);
System.out.println(obj.area());
Syste 19、m.out.println(obj.circumference());
}
}
8. 编写程序计算a=4c/b的值,处理当b=0时的情况(要求:使用Java的异常处理机制)。
解答:
public class Exam4 {
public void result(int x, int y) {
int a=0;
try{
a=4*y/x;
System.out.println("运算结果为:"+a); }
catch(Exception e){
System.out.println(e.toString()); }
20、 }
public static void main(String[] args) {
//Random r=new Random(); int b=r.nextInt(20),c=r.nextInt(20);
int b=0,c=2;
System.out.println("b="+b+" , c="+c);
Exam4 ex=new Exam4();
ex.result(b, c);
} }
9. 定义一个包含10个整数的一维数组并对数组进行初始化,输出该数组的所有元素,并求出该数组元素中的最大值及其对应下标值。
解答:
i






