Java面向对象入门习题

2021/7/22 1:06:25

本文主要是介绍Java面向对象入门习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.(中)设计一个圆类,具有属性:圆心(点)、半径。添加一个方法:判断一个圆是否包含一个点。

import java.awt.*;

class Circle{
     public Point center;//圆心坐标
     public double radius;//圆半径
    
    public Circle(Point center,double radius){
        this.center=new Point(center.x,center.y);
        this.radius=radius;
    }

    public boolean isInner(Point p){
        double dist = 0;

        dist=Math.sqrt((p.x-center.x)^2+(p.y-center.y)^2);
        return (dist<=radius? true:false);
    }
}

public class Work1 {
    public static void main(String[] args) {
        Circle c= new Circle(new Point(0,0),2);
        Point p = new Point(3,2);
        if(c.isInner(p))
            System.out.println("在圆内");
        else
            System.out.println("在圆外");
    }
}

2.(中)设计一个点类,具有属性:x、y坐标,具有方法:和另外一个点相加,得到一个新的点,新的点的x坐标是原来两个点的x坐标和,y是原来两个点的y坐标和

class point{
    public int x;
    public int y;

    public point(){}

    public point(int x,int y){
        this.x=x;
        this.y=y;
    }

    public void movepoint(int x,int y){
        this.x+=x;
        this.y+=y;
        System.out.println("坐标("+this.x+","+this.y+")");
    }
}
public class Work2 {
    public static void main(String[] args) {
        point p1 =new point(1,1);
        point p2 = new point(2,3);
        p1.movepoint(p2.x,p2.y);
    }
}

3.(中)设计一个MyArrays工具类,模拟实现:

/*
 *对数组array进行排序
 *@param array需要排序的数组
 *@param asc 排序的规则,true: 升序,false: 降序
 */
import java.util.Arrays;

class MyArrays {
    public static void sort(int[] array, boolean asc) {
        if(asc){
            for (int i = 0; i < array.length; i++) {
                for (int j = i+1; j <array.length ; j++) {
                    if(array[i]>array[j]){
                        array[i]^=array[j];
                        array[j]^=array[i];
                        array[i]^=array[j];
                    }
                }
            }
        }else{
            for (int i = 0; i < array.length; i++) {
                for (int j = i+1; j <array.length ; j++) {
                    if(array[i]<array[j]){
                        array[i]^=array[j];
                        array[j]^=array[i];
                        array[i]^=array[j];
                    }
                }
        }

        }
    }
}


public class Work3 {
    public static void main(String[] args) {
        int[] arr = new int[]{3, 5, 1, 6, 9};

        MyArrays.sort(arr, true);
        System.out.println(Arrays.toString(arr));
        MyArrays.sort(arr, false);
        System.out.println(Arrays.toString(arr));
    }

}

4.(易)定义一个网络用户类 要处理的信息有用户ID、用户密码、email地址。在建立类的实例时 把以上三个信息都作为构造函数的参数输入 其中用户ID和用户密码时必须的 缺省的email地址是用户ID加上字符串"@qf.com"

class User{
    String id;
    String pwd;
    String email;

    public User(String id,String pwd,String email){
        this.id=id;
        this.pwd=pwd;
        if(email=="")
            this.email=this.id+"@qf.com";
        else
            this.email=email;
    }
}

public class Work4 {
    public static void main(String[] args) {
       User user=new User("123456789","123","");
        System.out.println("ID:"+user.id);
        System.out.println("email:"+user.email);
    }
}

5.(非常难)小明交换牌:小明左手和右手分别拿了一张扑克牌,需要交换两只手中的扑克牌。最后小明展示交换后的扑克牌。扑克牌有花色和点数属性。

class Person {
    public Hand leftHand;
    public Hand rightHand;

    public void swap() {
        leftHand.swap(rightHand);
    }

    public void show() {
        System.out.println("左手:" + leftHand.getInfo());
        System.out.println("右手:" + rightHand.getInfo());
    }
}

class Hand {
    public Poker poker;

    public void swap(Hand other) {
        Poker temp = poker;
        this.poker = other.poker;
        other.poker = temp;
    }

    public String getInfo(){
        return this.poker.getInfo();
    }

}

class Poker {
    public String color;
    public String point;

    public String getInfo(){
        return color+point;
    }
}

public class Work5 {
    public static void main(String[] args) {
        Person person = new Person();

        Poker poker = new Poker();
        poker.color = "黑桃";
        poker.point = "K";
        Poker poker1 = new Poker();
        poker1.color = "h";
        poker1.point = "A";

        person.rightHand = new Hand();
        person.rightHand.poker = poker;

        person.leftHand = new Hand();
        person.leftHand.poker = poker1;

        person.show();
        person.swap();
        person.show();

        System.out.println(poker);
    }
}

6.定义一个类,该类有一个私有成员变量,通过构造方法将其进行赋初值并提供该成员的getXXX()和setXXX()方法

提示:假设有 private String name;

class Student{
    private String name;

    public Student(){}
    public Student(String name){
        this.name=name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
}

7.已知一个书签(BookMark)具有属性:页码和备注。根据以下代码段,完成类的设计。

public class Work7 {
    public static void main(String[] args) {
        //实例化书签对象,并给每一个属性赋值
        BookMark mark1 = new BookMark("天下无敌",20);
        BookMark mark2 = new BookMark("天上来敌",30);

        //比较两个书签的大小
        int result = mark1.compareTo(mark2);
        System.out.println(result);
    }
}

class BookMark{
    private String name;
    private int page;

    public BookMark(){}
    public BookMark(String name,int page){
        this.name=name;
        this.page=page;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setPage(int page){
        this.page=page;
    }
    public int getPage(){
        return page;
    }

    //比较方法,比较两个书签的页码
    public int compareTo(BookMark bm){
        if(this.page> bm.page){
            return 1;
        }else if(this.page==bm.page){
            return 0;
        }else{
            return -1;
        }
    }

    //第8题输出
    public String showInfo(){
        return "书签备注:"+name+",页码:"+page;
    }
}

8.在一个数组中,存储了5个书签(BookMark,直接使用上一题的类即可)。输出所有的页码范围在 [20, 50] 范围内的书签,输出格式如下

public class Work8 {
    public static void main(String[] args) {
        BookMark[] bm = new BookMark[5];
        bm[0]=new BookMark("东方月初",10);
        bm[1]=new BookMark("涂山红红",20);
        bm[2]=new BookMark("涂山雅雅",30);
        bm[3]=new BookMark("涂山容容",40);
        bm[4]=new BookMark("涂山苏苏",50);

        for (int i = 0; i < bm.length; i++) {
            if(bm[i].getPage()>=20&&bm[i].getPage()<=50){
                System.out.println(bm[i].showInfo());
            }
        }
    }
}


这篇关于Java面向对象入门习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程