资源描述
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);
for(int i=0;i<x.length;i++){
x[i]= scr.nextInt();
}
Arrays.sort(x);
for(int i=0;i<x.length;i++){
sum += x[i];
}
System.out.println("Max="+x[x.length-1]);
System.out.println("Min="+x[0]);
System.out.println("Average="+sum/x.length);
}
}
2.编写一个学生类Student:
属性包括:学号(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=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(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; }
public 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"+getComp() + "\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(
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));
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(IOException 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(String 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++){
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~999之间)的水仙花数。
解答:
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++;
}
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之间的正整数,打印输出这些随机数并求它们的最大值、最小值、平均值。
解答:
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<a.length;i++){
a[i]=(int)(Math.random()*200+1);
sum+=a[i];
System.out.print(a[i]+" ");
}
Arrays.sort(a);
System.out.println("\nmin="+a[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()方法,返回圆心坐标和半径。
(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 circumference(){
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());
System.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()); }
}
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个整数的一维数组并对数组进行初始化,输出该数组的所有元素,并求出该数组元素中的最大值及其对应下标值。
解答:
import java.util.*;
class Exam1{
public static void main(String args[]){
int i,max=0,index=0;
int a[]=new int[10];
Random rand=new Random();
for (i=0;i<10;i++)
{a[i]=rand.nextInt(100);
System.out.println(a[i]);
if (max<a[i])
{max=a[i];index=i;}
}
System.out.println("max="+max+",index="+index);
}
}
说明:数组初始化方法不限,完成数组初始化即可。
10. 试编写应用程序从键盘输入一个整数x,求出≤x的所有素数,并将这些数在屏幕上5个一行显示出来。
解答:
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
int i,j,k=0;
Scanner scr = new Scanner(System.in);
int x = scr.nextInt();
for(i=2;i<=x;i++){
for(j=2;j<=i/2;j++)
if(i%j==0)break;
if(j>i/2){
System.out.print(i+" ");
k++;
if(k%5==0)
System.out.print("\n");
}
}
}
}
11. 采用异常处理的方式编写程序,定义一个整型数组,输出所有元素,要求处理数组越界异常ArrayIndexOutOfBoundsException。
解答:
public class Exam4{
public static void main(String[] args){
int[] myarrays = {1,2,3,4,5,6,7,8};
try{
for(int i=0;i<myarrays.length;i++){
System.out.println(myarrays[i]);
}
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.toString());
}
}
}
12. 编程求出2~n之间的所有素数。n由键盘输入。
解答:
import java.io.*;
class Exam3_1{
public static void main (String[] args) throws IOException{
String str;
int n,i,j;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
str = in.readLine();
in.close();
n=Integer.valueOf(str).intValue();
System.out.println("2");
for(i=3;i<=n;i=i+2) {
int k=(int)Math.sqrt(i)+1;
for(j=2;j<=k;j++)
if(i%j==0) break;
if(j>=k) System.out.println(i);
}
}}
13. 按以下要求编程
(1)编写Animal类,定义leg属性、有参构造函数对leg赋初值,定义空的sound()方法;
(2)编写Flyable接口,定义speed( )方法;
(3)编写Glede类继承Animal类并实现Flyable接口,定义speed属性,有参构造方法对leg和speed赋初值,speed( )方法返回speed属性,sound( )方法输出“嘎……”。
(4)编写测试程序,定义包含2个元素的Glede对象数组,分别调用speed( )方法和sound( )方法。
解答:
class Animal{
int leg;
public Animal(){}
public Animal(int leg){this.leg=leg;}
}
interface Flyable{
int speed();
}
class Glede extends Animal implements Flyable{
int speed;
public Glede(int speed){
super(2);
this.speed=speed;
}
public int speed(){
return speed;
}
public void sound( ){
System.out.println("嘎……");}
}
class exam3_3{
public static void main(String args[]){
Glede[] glede=new Glede[2];
glede[0]=new Glede(200);
glede[1]=new Glede(150);
System.out.println(glede[0].speed());
glede[0].sound();
System.out.println(glede[1].speed());
glede[1].sound();
} }
14. 编写程序,将从命令行获得字符串转换为整型值,处理当args[0]=“56k9”时的情况(要求捕获异常NumberFormatException并处理)
解答:
class Exam3_4{
public static void main(String args[]){
try{
String str=args[0];
int n=Integer.parseInt(str);
}catch(NumberFormatException ne)
{System.out.println(ne);} } }
15.连续写下从整数1开始到某个正整数N之间的所有整数时,能得到如下的数字序列:1234567891011121314151617181920212223……,请编写程序输入正整数N,并计算得到的数字序列中数字字符的个数,程序的运行效果如下图所示。
解答:
import java.util.Scanner;
public class Ex {
public static void main(String[] args)throws Exception
{
int i=1,sum=0;
Scanner sr = new Scanner(System.in);
System.out.print("请输入正整数N:");
int n=sr.nextInt();
for(i=1;i<=n;i++){
sum+=(i+"").length();
}
System.out.print("序列中的数字字符个数为:");
System.out.println(sum);
}
}
16. 按以下要求定义类Point表示平面上的一个点,定义类Circle表示平面上的一个圆,具体要求如下:
(1) Circle类的实例属性:圆心x坐标(xpoint)、圆心y坐标(ypoint)和半径(radius)。
(2) Point类的实例属性:点的x坐标(x)、点的y坐标(y)。
(3) 分别编写Point类和Circle类的构造方法,对属性进行初始化。
(4) 在Point类中编写方法计算两个点之间的距离。
方法格式:public static double distance(Point p1,Point p2)
(5) 在Circle类中编写方法判断一个点是否在圆内。
方法格式:public static boolean isInside(Circle c,Point p)
(6) 在测试类中实例化类的对象并调用方法完成信息的输出。
解答:
public class Circle {
private double xpoint;
private double ypoint;
private double radius;
public Circle(double xpoint, double ypoint, double radius) {
this.xpoint = xpoint;
this.ypoint = ypoint;
this.radius = radius;
}
public static boolean isInside(Circle c,Point p){
double s=(c.xpoint-p.x)*(c.xpoint-p.x) +(c.ypoint-p.y)*(c.ypoint-p.y);
if (s<(c.radius*c.radius))
return true;
else
return false;
}
}
public class Point {
public double x;
public double y;
public Point(double x,double y){
this.x=x;
this.y=y;
}
public static double distance(Point p1,Point p2){
double s=(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
return Math.sqrt(s);
}
}
public class Tester {
public static void main(String[] args){
Point p1= new Point(1,1);
Point p2= new Point(2,2);
Circle c= new Circle(0,0,2);
double s=Point.distance(p1, p2);
System.out.println(s);
System.out.println(Circle.isInside(c, p1));
System.out.println(Circle.isInside(c, p2));
}
}
17. 请编写一个应用程序创建两个线程,一个线程打印输出一个1~500之间所有奇数,另外一个线程打印输出600~1000之间所有偶数,第一个线程每输出一个数休眠1000毫秒,第二个线程每输出一个数休眠2000毫秒,在主程序中启动这两个线程。
解答:
class Thread1 extends Thread{
public Thread1(String msg){
super(msg);
}
public void run(){
int sum=0;
for(int i=1;i<=500;i++){
if(i % 2 !=0)
System.out.println(i);
try{
Thread.sleep(1000);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}
}
class Thread2 extends Thread{
public Thread2(String msg){
super(msg);
}
public void run(){
int sum=0;
for(int i=600;i<=1000;i++){
if(i % 2 ==0)
System.out.println(i);
try{
Thread.sleep(2000);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
}
}
}
public class Test{
public static void main(String[] args){
Thread1 x = new Thread1("Thread1");
Thread2 y = new Thread2("Thread2");
x.start();
y.start();
}
}
展开阅读全文