Java程序设计期末考试整理

  • 格式:doc
  • 大小:98.50 KB
  • 文档页数:11
public static void main(String[] args) {
Convert2to10 c =new Convert2to10 ();
String number =JOptionPane.showInputDialog("Input the Number ");
try
{
c.convert( number);
1、(选择结构)计算个人所得税。设某人月收入为x元(x由键盘输入),假设个人所得税征收方法如下:
(1)当800<x≤1300时,应征税为(x-800)*5%;当1300<x≤2800时,应征税为(x-800)*10%;
(2)当2800<x≤5800时,应征税为(x-800)*15%;当5800<x≤28000时,应征税为(x-800)*20%;
}
}
Shape r =new Rectangle (20.0,10.0);
r.area();
r.circumference();
Shape c =new Circle (5.0);
c.area() ;
c.circumference() ;
Shape s =new Square (6.0);
s.area() ;
}
catch (MyException e)
{
JOptionPane.showMessageDialog(null,e.toString() ,"output",JOptionPane.PLAIN_MESSAGE);
}
}
}
3、(继承与多态)
(3.1静态多态方法重载实现)
(重载—多态)
public class OverLoadDemo {
(3)当28000<x时,应征税为(x-800)*30%
(if else实现)
import javax.swing.*;
public class Tax1 {
public static void main(String[] args) {
String salary =JOptionPane.showInputDialog("","请输入您的工资");
{
this.a=a ;
}
public double perimeter()
{
return 4*a;
}
public double area()
{
return a*a;
}
}
测试类:
public static void print(Shape shape){
System.out.println(shape.getClass()+":"+"面积:"+shape.area());
public double area();
}
class Rectangle implements Shape
{
double a ;
double b ;
public Rectangle(double a ,double b)
{
this.a=a;
this.b=b;
}
public double perimeter()
{
return 2*(a+b);
}
public double area()
{
return a*b;
}
}
class Circle implements Shape
{
public static final double PI=3.14;
double r ;
public Circle(double r)
{
print(shape);
shape =new Circle (4);
print(shape);
shape =new Square (5);
print(shape);
}
4、(GUI命令行按钮或菜单使用)
4.1(事件处理)当需要按钮被按下时执行一定的任务,就要向按钮注册动作事件监听器,并为按钮编写动作事件处理代码,即编写实现ActionLisener监听接口的监听器类,并实现actionPerforned方法。设计一个Java GUI应用程序,当点击按钮时,记录点击按钮和点击次数,并显示在窗体中。
class Animal
{
void Walk()
{
System.out.println("调用动物类的走路walk方法");
}
void Eat()
{
System.out.println("调用动物类的吃Eat方法");
}
}
class Goat extends Animal
{
void Eat()
{
System.out.println("羊吃草");
Animal a2 =new Wolf();
a2.Eat() ;
a2.Walk();
}
3.22(抽象类—多态)设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square),能够利用抽象类和多态性计算几何图形的面积和周长,并显示出来。
abstract class Shape
{
this.x=x;
this.y=y;
}
void circumference()
{
System.out.println("矩形周长是"+2*(x+y));
}
void area()
{
System.out.println("矩形面积是"+(x*y));
}
}
class Circle extends Shape
}
}
class Wolf extends Animal
{
void Eat()
{
System.out.println("狼吃肉");
}
}
测试类:
public static void main(String[] args) {
Animal a1 =new Goat();
a1.Eat();
a1.Walk();
}
public void print(int a ,int b)
{
System.out.println("两个参数a="+a+"b="+b);
}
}
(3.2运行时多态方法覆盖实现)
3.21(类继承—多态)编写动物世界的继承关系代码。动物(Animal)包括山羊(Goat)和狼(Wolf),他们吃(eat)的行为不同,山羊吃草,狼吃肉,但走路(walk)的行为是一致的。通过继承实现以上需求,并编写AnimalTest测试类进行测试。
}
else if(x>2800&&x<=5800)
{
System.out.println("您应该交税"+(x-800)*0.15);
}
else if(x>5800&&x<=28000)
{
System.out.println("您应该交税"+(x-800)*0.2);
}
else
{
System.out.println("您应该交税"+(x-800)*0.3);
}
}
class Square extends Shape
{
protected double a;
public Square(double a)
{
this.a=a;
}
void circumference()
{
System.out.println("正方形周长是"+4*a);
}
void area()
{
System.out.println("正方形面积是"+a*a);
{
protected double r;
public Circle(double r)
{
this.r =r;
}
void circumference()
{
System.out.println("园周长是"+2*PI*r);
}
void area()
{
System.out.println("圆面积是"+PI*r*r);
{
Arr[j]=str.charAt(j)-48;
if(Arr[j]!=1&&Arr[j]!=0)
{
b=false;break;
}
}
if(b)
{
for(int j=0;j<len ;j++)
{
Arr[j]=str.charAt(j)-48;
result+=Arr[j]*Math.pow(2,(len-1-j));
int x= Integer.parseInt(salary);
if(x>800&&x<=1300)
{
System.out.println("您应该交税"+(x-800)*0.5);
}
else if(x>1300&&x<=2800)