java程序设计题

《面向对象java语言程序设计》
实验参考答案

1、求一维int型数组A中元素的最大值,最小值,并将并将其输出。
int A[]={23,76,45,21,63,90,46,83}
public class Test1 {
public static void main(String[] args) {
int[] A = { 23,76,45,21,63,90,46,83 };
for (int i = 0; i <= A.length - 1; i++) {
for (int j = i + 1; j <= A.length - 1; j++) {
if (A[i] > A[j])
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
System.out.println("最小值为:" + A[0] + " , 最大值为:" + A[A.length - 1]);
}
}
2、编写打印三个矩形的程序。
public class Test2
{
public static void drawRectangle(int x,int y)
{
for(int i=0;i{
for(int j=0;j{
System.out.print(" * ");
}
System.out.println(); //换行
}
System.out.println();
}
public static void main(String [] args)
{
drawRectangle(3,5);
drawRectangle(2,4);
drawRectangle(6,10);
}
}
3、定义一个类Exercise2010。该类有一个表示年份的int型成员变量year,并包含以下两个方法:
public void set(int y); // 设置成员变量year的值。
public boolean isLeap(); // 报告year表示的年份是否为闰年。
然后再定义一个应该程序起始类TestExercise2010测试该类。
闰年是指能被400整除、或者能被4整除但不能被100整除的年份。
class Exercise2010{
int year;
public void set (int y){
year=y;
}
public Boolean isleap(){
if ((year%4==0 && year%100!=0) || (year%400==0))
return true;
else
return false;
}
}

public class TestExercise2010{
public static void main(String [] args){
Exercise2010 exer = new Exercise2010();
exer.set(2012);
boolean flag=exer. isleap();
if (flag == true)
System.out.println(2012 +"是闰年 ");
else
System.out.println(2012 +"不是闰年 ");
}
}
4定义student类,其中包括四个私有变量(name,age,sex,score)、定义赋初值方法和show()方法。各成员的含义如下:
变量name为字符串类型String,用于存储学生的姓名。
变量age为int类型,用于存储学生的年龄。
变量sex为boolean类型,用于存储学生的性别,男生为false,女生为true。
变量score为double类型,用于存储学生的成绩。
赋初值方法包括四个参数,用于为变量(name,age,sex和score)赋值。
show()方法无参数,用于输出变量(name,age,sex和score)的值。
class Student{
private String name;
private int age;
private boolean sex;
private double score;
public void set(String name , int age , boolean sex , double score){
https://www.360docs.net/doc/9211460351.html,=name;
this.age=age;
this.sex=sex;
this.score=score;
}
public void show(){
System.out.println("我的姓名是"+name);
System.out.println("我的年龄是"+age);
if (sex == true)
System.out.println("我是强壮的男子汉");
else
System.out.println("我是美丽的女学

生");
System.out.println("我的数学成绩是"+score);
}
}
public class Test4{
public static void main(String [] args){
Student student =new Student();
student.set("姓名",19,true,97.0);
//以自已的个人信息赋值
student.show();
}
}
5、编写程序, 在输入非数字字符时,产生NumberFormatException。捕获异常,发生异常时提示用户越界信息。
import java.io.*;
public class Test5{
public static void main(String[] args) {
int i=1;
while (i!=0)
{
System.out.println("请输入一个数字:(输入0)结束)");
try
{
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(bfr.readLine());
System.out.println("输入的数字是"+i);
}
catch(IOException e)
{
System.out.println("I/O error");
}
catch (NumberFormatException e)
{
System.out.println("输入的数字格式错误!");
}
}
}
}
6、 自定义异常类,在输入的数据大于100,或小于0时,定义一个异常来捕捉。
import java.io.*;
class NumberlimitedExceptiion extends Exception
{
NumberlimitedExceptiion()
{
System.out.println("Out of the limited !");
}
}

public class Test6
{
public static void prime(int m) throws NumberlimitedExceptiion
{
if(m>100||m<0)
{
NumberlimitedExceptiion limitexcep=new NumberlimitedExceptiion();
throw limitexcep;
}
}
public static void main(String[] args)
{
int i;
System.out.println("请输入一个数字: ");
try
{
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(bfr.readLine());
prime(i);
System.out.println("输入正确数字"+i);
}
catch(IOException e)
{
System.out.println("I/O error");
}
catch(NumberlimitedExceptiion e)
{
System.out.println("异常名称:"+e.toString());
}
}
}

相关文档
最新文档