PTA java 函数题

 是否偶数
public static boolean isOdd(int data){
    return data%2==0?true:false;
}
根据派生类写出基类(Java)
 public People() {
        
    }
    public People(String id,String name) {
        this.id=id;
        this.name=name;
    }
    public void setId(String id){
        this.id=id;
    }
    public String getId(){
         return this.id;
}
    public void setName(String name) {
        this.name=name;
    }
     public String getName() {
        return this.name;
 }
    public void say() {
        // TODO Auto-generated method stub
        System.out.println("I'm a person! My name is " + this.name + ".");
        
    }

使用继承设计:教师类。
class Teacher extends Person{//继承
    String professional;//定义专业名
    Teacher(String name, int age, String professional) {
        super(name, age);
        this.professional=professional;
    }
    void work(){
        System.out.println(professional);
        System.out.println("教师的工作是教学。");
    }
}
设计一个Duck类及其子类
class Duck {

    public void display() {
        // TODO Auto-generated method stub
        }
    public void quack() {
        System.out.println("我会呱呱呱");
    }
    public void swim() {
        System.out.println("我会游泳");
    }
    public void fly() {
        System.out.println("我会飞");
    }
}

//RedheadDuck类的定义
class RedheadDuck extends Duck { 
    public void display() {
        System.out.println("我是一只红头鸭");
    }
}

//MallardDuck类的定义
class MallardDuck extends Duck { 
    public void display() {
        System.out.println("我是一只绿头鸭");
    }
}
 jmu-Java-03面向对象基础-覆盖与toString
public String toString() {

        return super.toString()+"-"+company.toString()+"-"+salary;

    }
重写父类方法equals


public boolean equals(Object obj) {

    boolean boo=false;

    if(obj instanceof Student) {

        Student s=(Student) obj;

        if(s.id==this.id)

        {

            boo=true;

        }

    }

    return boo;

}
从抽象类shape类扩展出一个圆形类Circle
class Circle extends shape{
    private double radius;
    public Circle(double radius){
        this.radius =radius;
    }
    
    public double getArea(){
        return Math.PI *radius*radius;
    }
    public double getPerimeter( ) {
        return 2*Math.PI*radius;
    }
}
创建一个直角三角形类实现IShape接口
class RTriangle implements IShape{
    double a,b;
    
    public RTriangle(double a, double b) {
        super();
        this.a = a;
        this.b = b;
    }
    public  double getArea() {
        return 0.5*a*b;
    }
    public double getPerimeter() {
        return a+b+Math.sqrt(a*a+b*b);
    }
}
jmu-Java-06异常-finally
System.out.println("resource open success");
}catch(Exception e){
    System.out.println(e);
}
try{
    resource.close();
    System.out.println("resource release success");
}catch(RuntimeException e){
    System.out.println(e);
}
异常-多种类型异常的捕获
catch(Exception e){
                if (choice.equals("number"))
                    System.out.println ("number format exception");
                if (choice.equals("illegal"))
                    System.out.println ("illegal argument exception");
                if (choice.equals("except"))
                    System.out.println ("other exception");
                System.out.println (e);
            }
求圆面积自定义异常类
class Circle{
    double r;
    Circle(double r) {
        this.r = r;
    }
    public double area() throws CircleException{
        if(r<0)
        {//判断异常是否会产生
            CircleException e = new CircleException(r);//在创建对象时将半径赋值给CircleException对象
            throw e;//抛出异常
        }
        return 3.14*r*r;
    }
}
class CircleException extends Exception {
    double r;
    CircleException(double r){//有参数的构造方法
        this.r = r;
    }
    public void print () {//被调用的print语句
        System.out.println("圆半径为"+r+"不合理");
    }
}
jmu-Java-07多线程-Thread
class MyThread extends Thread{
    int i;
    public MyThread(int i){
        this.i=i;
    }
    public void run () {
        for(int r=0;r<i;r++) {
            System.out.println(r);
        }
        System.out.println(Thread.currentThread().getName()+" "+isAlive());
    }
    
}
 jmu-Java-07多线程-PrintTask
class PrintTask implements Runnable
{
    private int n;
    public PrintTask(int n)
    {
        this.n=n;
    }
    public void run()
    {
        for(int i=0;i<n;i++)
        {
            System.out.println(i);
        }
        System.out.println(Thread.currentThread().getName());
    }
}

 数组工具类的设计

class MyArrays
{
     public static void printArray(int a[]) 
     {
          for(int i=0;i<a.length;i++) 
          {
               if(i==a.length-1) 
               {
                    System.out.println(a[i]);
               }
               else
                    System.out.print(a[i]+",");
          }
     }
     public static void sortArray(int a[]) 
     {
          for(int i=0;i<a.length-1;i++) 
          {    
               for(int j=0;j<a.length-1-i;j++) 
               {
                    int temp=0;
                    if(a[j]>a[j+1]) 
                    {
                         temp=a[j];
                         a[j]=a[j+1];
                         a[j+1]=temp;
                    }
               }
          }
     }
     public static int sumOfArray(int a[]) 
     {
          int sum=0;
          for(int i=0;i<a.length;i++) 
          {
               sum=sum+a[i];
          }
          return sum;
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值