java上机实验答案与解析

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Circle c2=new Circle;
Area());
Area());
}
}
实验三 类的继承和多态性
1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长
(2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。
case 1: int b[]=null;k=b[0];break;
case 2: int c[]=new int[2];k=c[9];break;
case 3: char ch="abc".charAt(99);break;
}
}
catch(ArithmeticException e){
}
}
catch(ArrayIndexOutOfBoundsException e){
</applet>
第四步 将记事本文件名命名为
第五步 打开
实验二 类的定义
1.编写Java应用程序,自定义Point类,类中有两个描述坐标位置的double变量x,y,利用构造方法,实现对Point 对象p1,p2初始化,p1和p2对应坐标分别为(15,20),(10,30);定义方法getX(),getY()分别获得点的横坐标和纵坐标;定义方法setX(),setY()分别获得点的横坐标和纵坐标;并且把p1和p2输出;
FileInputStream in=new FileInputStream(f1);
File f2 = new File("e:\\“学号”+“姓名”.doc");
FileOutputStream out=new FileOutputStream(f2);
在主方法中产生半径为5的圆。
interface ShapePara {
double getArea(double r);
double getCircumference(double r);
}义图形类Shape,该类中有获得面积的方法getArea();定义长方形类Rect,该类是Shape的子类,类中有矩形长和宽的变量double a,double b,设置长和宽的方法setWidth()、setHeight(),使用getArea()求矩形面积;利用getArea方法实现题1中圆面积的求解。
3.编写的Java应用程序,程序完成的功能是:完成文件的复制,复制以后的文件的名称为自己的学号姓名.doc。
import .*;
public class BinIoDemo{
public static void main(String[] args) throws IOException{
File f1=new File("e:\\");
for(inti=0;i<4;i++){
intk;
switch(i){
case0:intzero=0;k=911/zero;break;
case1:intb[]=null;k?=b[0];break;
case2:int c[]=new int[2];k=c[9];break;
case3:charch="abc".charAt(99);break;
JAVA上机实验题答案与解析
实验一 Java程序编程
1.编写一个Java应用程序,输出内容为Hello!。
注:文件位置位于e:\2:\ 编译:(1)e:(2)cd 2 (3)javac (4)java Hello
2.编写一个Java小应用程序,输出内容为我一边听音乐,一边学Java。
第一步编写
import .*;
public static void main(String args[]) {
Rect rect=new Rect();
double w=,h=;
(w);
(h);
}
}
答案:
圆的的面积:
矩形对象的宽: 高:
3. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。
public class Circle {
double s,r;
public Circle(double r){
=r;
}
double getArea(){
=pi*r*r;
return s;
}
public static void main(String[] args) {
Circle c1=new Circle(3);
=name;
}
String getKind(){
return name;
}
int getCount(){
return count;
}
}
public class Fish extends Animal{
static int countFish;
Fish(){countFish++;}
int getCount(){
a=width;
}
void setHeight(double height) {
b=height;
}
double getWidth(){
returna;
}
double getHeight(){
returnb;
}
double getArea(){
area=a*b;
return area;
}
}
public class A {
=y;
}
p源自文库blic static void main(String[] args) {
Point p1=new Point(15,20);写Java应用程序,自定义Circle类,类中有两个double 变量r,s,一个类变量pi,利用构造方法实现对半径是3和的初始化,自定义getArea方法实现圆面积求解;
class Shape {
double getArea(double r){
return0;
}
}
public class Rect extends Shape {
doublea,b,area;
Rect(double width,double heigh){
a=width;
b=height;;
}
void setWidth(double width) {
import .*;
public class MyApplet extends Applet{
public void paint(Graphics g){
("我一边听音乐,我一边做java",25,25);
}
}
第二步 在DOS环境中编译(....javac )
第三步 使用记事本编写
<applet code= height=200 width=120>
public class Animal {
String name;
int legs;
staticint count;
Animal(){count++;}
void setLegs(int legs){
=legs;
}
int getLegs(){
return legs;
}
void setKind(String name){
public class A {
public static void main(String args[]){
Fish f=new Fish();
Tiger t=new Tiger();
SouthEastTiger st=new SouthEastTiger();
}
}
实验四 异常处理
1.建立exception包,编写程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。
}
}
}
运行结果:
请输入一个数
80
银行里还剩余:
实验五 输入输出
1.编写的Java应用程序,程序完成的功能是:首先向中写入自己的学号和姓名,读取中信息并将其显示在屏幕上。
import .*;
public class TextRw {
public static void main(String[] args) throws IOException {
该类包含有成员变量:
radius:public 修饰的double类型radius,表示圆的半径。
x:private修饰的double型变量x,表示圆心的横坐标。
y:protected修饰的double型变量y,表示圆心的纵坐标。
包含的方法有:
Circle(double radius) 有参构造方法。以形参表中的参数初始化半径,圆心为坐标原点。 double getRadius():获取半径为方法的返回值。void setCenter(double x, double y):利用形参表中的参数设置类Circle的圆心坐标。void setRadius(double radius):利用形参表中的参数设置类Circle的radius域。
提示: InsufficientFundsException,NagativeFundsException为自定义的类,分别产生余额不足异常和取款为负数异常,需继承Exception类。
通过输出结果了解java异常的产生,并将该Java程序放在exception包中。
package Exception;;
public class Point {
double x,y;
Point(double x,double y){
=x;
=y;
}
double getX(){
return x;
}
double getY(){
return y;
}
void setX(double x){
=x;
}
void setY(double y){
}
}
}
}
}
运行结果:
/ by zero
null
9
String index out of range: 99
2.建立exception包,建立Bank类,类中有变量double balance表示存款,Bank类的构造方法能增加存款,Bank类中有取款的发方法withDrawal(double dAmount),当取款的数额大于存款时,抛出InsufficientFundsException,取款数额为负数,抛出NagativeFundsException,如new Bank(100),表示存入银行100元,当用方法withdrawal(150),withdrawal(-15)时会抛出自定义异常。
}
}
package Exception;
public class TestException {
public static void main(String[] args){
for(int i=0;i<4;i++){
try{
int k;
switch(i){
case 0: int zero=0;k=911/zero;break;
public class A{
public static void main(String args[] ){
Bank b=new Bank(100);
Scanner sc=new Scanner;
try {
());
}
catch(InsufficientFundsException e){
}
catch(NagativeFundsException e){
return countFish;
}
}
public class Tiger extends Animal{
static int countTiger;
Tiger(){countTiger++;}
int getCount(){
return countTiger;
}
}
public class SouthEastTiger extends Tiger{}
File f=new File("f:\\");
FileWriter fw=new FileWriter(f);
("学号 姓名");
();
FileReader fr=new FileReader(f);
int i;
while((i=())!=-1)
();
}
}
2.编写的Java应用程序,程序完成的功能是:首先读取源程序文件,再通过键盘输入文件的名称为,把的源程序存入
相关文档
最新文档