2024年1月25日发(作者:)
1. 定义一个包括10个元素一维整型数组,通过从键盘输入的10个整数对数组进行初始化,将数组中的元素按小到大排序后在屏幕上显示,求出该数组中元素的最小值、最大值以及平均值并在屏幕上显示输出。
解答:
import r;
import ;
public class Test{
public static void main(String[] args) {
int[] x = new int[10];
double sum =0;
Scanner scr = new Scanner();
for(int i=0;i<;i++){
x[i]= t();
}
(x);
for(int i=0;i<;i++){
sum += x[i];
}
n("Max="+x[-1]);
n("Min="+x[0]);
n("Average="+sum/);
}
}
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){
=id;
=name;
=eng;
=math;
=comp;
sum(); //计算总成绩 }
public Student(Student s){
=;
=new String();
=;
=;
=;
sum(); //计算总成绩 }
=id; } public void setId(String id){
public void setName(String name){
=name; }
public void setEng(int eng){
=eng; sum(); }
public void setMath(int math){
=math; sum(); }
public void setComp(int comp){
=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(){ =eng+math+comp; }
public String toString(){
return getId() + "t"+getName() + "t"+getEng() +
"t"+getSum();} }
3. 设计一个一元二次方程类,并为这个类添加异常处理。
解答:
public class Operation {
public static void main(String[] args)
{
String s="";
double a,b,c,r1,r2;
("求二元一次方程的根") ;
("请键入第一个系数a") ;
try
{
BufferedReader in =new BufferedReader(
new InputStreamReader());
s=ne();
}catch(IOException e){}
a=ouble(s);
("请键入第二个系数b") ;
try
{
BufferedReader in =new BufferedReader(
new InputStreamReader());
"t"+getMath() +"t"+getComp() +
s=ne();
}catch(IOException e){}
b=ouble(s);
("请键入第三个系数c") ;
try
{
BufferedReader in =new BufferedReader(
new InputStreamReader());
s=ne();
}
4.编写一个应用程序创建两个线程,一个线程打印输出1~1000之间所有3的倍数,另外一个线程打印输出1000~2000之间所有5的倍数。
解答:
class Thread1 extends Thread{
}
class Thread2 extends Thread{
public Thread2(String msg){
}
}}
super(msg);
int sum=0;
for(int i=1000;i<=2000;i++){
}
if(i % 5 ==0)
n(i);
public Thread1(String msg){
}
}
int sum=0;
for(int i=1;i<=1000;i++){
}
if(i % 3 ==0)
n(i);
super(msg);
}
}catch(IOException e){}
c=ouble(s);
r1=(-b+(b*b-4*a*c))/(2*a);
r2=(-(b*b-4*a*c))/(2*a);
("该二元一次方程的根为:"+r1+"和"+r2) ;
public void run(){
public void run(){
public class Exam5{
}
public static void main(String[] args){
}
Thread1 x = new Thread1("Thread1");
Thread2 y = new Thread2("Thread2");
();
();
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(((i,3)+(j,3)+(k,3))==n)
n("找到第"+m++ +"个水仙花数:"+n);
n++;
}
m=1;
// 或者使用下面的方法
for (n=100;n<1000;n++){
i=n/100;
j=(n-i*100)/10;
k=n%10;
if(((i,3)+(j,3)+(k,3))==n)
n("找到第"+m++ +"个水仙花数:"+n);
n++;
}
}
}
6. 编写程序随机生成10个1到200之间的正整数,打印输出这些随机数并求它们的最大值、最小值、平均值。
解答:
import ;
public class Test{
public static void main(String[] args){
int[] a= new int[10];
double sum=0;
for(int i=0;i<;i++){
a[i]=(int)(()*200+1);
sum+=a[i];
(a[i]+" ");
}
(a);
n("nmin="+a[0]);
n("max="+a[-1]);
n("average="+(sum/));
}
}
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 *radius*radius;
}
public double circumference(){
return 2* *radius;
}
public String toString(){
return "圆心:("+xpoint+","+ypoint+")"+" 半径:"+radius;
}
}
public class Test{
public static void main(String[] args) {
Circle obj = new Circle(0,0,100);
n(obj);
n(());
n(ference());
}
}
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;
n("运算结果为:"+a); }
catch(Exception e){
n(ng()); }
}
public static void main(String[] args) {
//Random r=new Random(); int b=t(20),c=t(20);
int b=0,c=2;
n("b="+b+" , c="+c);
Exam4 ex=new Exam4();
(b, c);
} }
9. 定义一个包含10个整数的一维数组并对数组进行初始化,输出该数组的所有元素,并求出该数组元素中的最大值及其对应下标值。
解答:
import .*;
class Exam1{
10. 试编写应用程序从键盘输入一个整数x,求出≤x的所有素数,并将这些数在屏幕上5个一行显示出来。
解答:
import r;
public class Test{
}
11. 采用异常处理的方式编写程序,定义一个整型数组,输出所有元素,要求处理数组越界异常ArrayIndexOutOfBoundsException。
解答:
public static void main(String[] args) {
}
int i,j,k=0;
Scanner scr = new Scanner();
int x = t();
for(i=2;i<=x;i++){
if(i%j==0)break;
if(j>i/2){
}
(i+" ");
k++;
if(k%5==0)
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]=t(100);
n(a[i]);
本文发布于:2024-01-25 20:34:36,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1706186076245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |