java实验5面向对象练习题

本博文源于课上的题目,已经进行到第五周了,java代码我写的非常累,四道题目只完成了三题,因此将这三题放出来,一道题目一道代码,内容非常死板,只需要按部就班就行了

1.第一道题

1.1 题目再现

在这里插入图片描述
在这里插入图片描述

1.2 代码

package com.company;

public class Fan {
    
    
    int SLOW = 1;
    int MEDIUM = 2;
    int FAST = 3;
    private  int speed = SLOW;
    private boolean on = false;
    private double radius = 5;
    String color = "blue";
    /*public Fan(){
        speed = SLOW;
        on = false;
        radius = 5;
        color = "blue";
    }*/
    public Fan(){
    
    
        this.speed = SLOW;
        this.on = false;
        this.radius = 5;
        this.color = "blue";
    }
    public Fan(int speed,boolean on ,double radius,String color){
    
    
        this.speed = speed;
        this.on = on;
        this.radius= radius;
        this.color = color;
    }
    public String toString(){
    
    
        if(on){
    
    
            return "fan speed" + this.speed + "fan color:" + this.color + "fan radius" + this.radius;
        }else{
    
    
            return "fan was colsed,fan's color:" + this.color + "fan radius" + this.radius;
        }
    }
    public void setColor(String color) {
    
    
        this.color = color;
    }

    public void setOn(boolean on) {
    
    
        this.on = on;
    }

    public void setRadius(double radius) {
    
    
        this.radius = radius;
    }

    public void setSpeed(int speed) {
    
    
        this.speed = speed;
    }

    public int getSpeed() {
    
    
        return speed;
    }

    public String getColor() {
    
    
        return color;
    }

    public double getRadius() {
    
    
        return radius;
    }

    public boolean isOn() {
    
    
        return on;
    }
}

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Fan f1 = new Fan();
        Fan f2 = new Fan(2,true,10,"yellow");
        System.out.println(f2.toString());
    }


}

2、第二道题

2.1 原题再现

在这里插入图片描述
在这里插入图片描述

2.2 代码

package com.company;

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    

        System.out.println("please input two parmeter space divide:");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        new NumOP(a,b);
    }


}
package com.company;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class NumOP {
    
    
    public static int add(int a,int b){
    
    
        return a+b;
    }
    public static int subtract(int a,int b){
    
    
        return a-b;
    }
    public static int multiply(int a,int b){
    
    
        return a*b;
    }
    public static double divide(int a,int b){
    
    
        return (double) a/b;
    }
    public NumOP(int a,int b){
    
    
        System.out.println(a+"+"+b+"="+add(a,b));
        System.out.println(a+"-"+b+"="+subtract(a,b));
        System.out.println(a+"*"+b+"="+multiply(a,b));
        float c =(float)a;
        DecimalFormat df = new DecimalFormat("0.00");
        String result = df.format(divide(a,b));
        System.out.println(a+"/"+b+"="+result);

    }
}

3、第三题

3.1 原题再现

在这里插入图片描述
在这里插入图片描述

3.2 代码测试

package com.company;

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        MyInteger myInteger = new MyInteger(5);
        System.out.println(myInteger.getValue() + "是否偶数" + myInteger.isEven());
        System.out.println(myInteger.getValue() + "是否奇数" + myInteger.isOdd());
        System.out.println(myInteger.getValue() + "是否素数" + myInteger.isPrime());
        System.out.println("12 is Prime?" + MyInteger.isEven(10));
        System.out.println("12 is Even?" + MyInteger.isOdd(10));
        System.out.println("12 is Prime?" + MyInteger.isPrime(10));
        System.out.println("is Equal?" + myInteger.equals(5));
        MyInteger myInteger1 = new MyInteger(10);
        System.out.println("is Equal?" + myInteger.equals(myInteger1));
        char[] chars = {
    
    '1', '2', '3'};
        System.out.println(MyInteger.parseInt(chars));
        System.out.println(MyInteger.parseInt("123"));
    }


    }
package com.company;

import java.text.DecimalFormat;

public class MyInteger {
    
    
    private int value;
    public MyInteger(int v){
    
    
        this.value = v;
    }

    public int getValue() {
    
    
        return value;
    }

    public void setValue(int value) {
    
    
        this.value = value;
    }
    public  boolean isEven(){
    
    
        return this.value%2==0;
    }
    public static boolean isEven(int a){
    
    
        return a%2==0;
    }
    public boolean isOdd(){
    
    
        return this.value%2==1;
    }
    public static boolean isOdd(int a){
    
    
        return a%2==1;
    }
    public boolean isPrime(){
    
    
        if(this.value<2) return false;
        for(int i =2;i<Math.sqrt((double)this.value);i++){
    
    
            if(this.value%i==0){
    
    
                return false;
            }
        }
        return true;
    }
    public static boolean isPrime(int num){
    
    
        if(num<2) return false;
        for(int i =2;i<Math.sqrt((double)num);i++){
    
    
            if(num%i==0){
    
    
                return false;
            }
        }
        return true;
    }
    public boolean equals(int a){
    
    
        return this.value == a;
    }
    public boolean equals(MyInteger myInteger){
    
    
        return this.value == myInteger.value;
    }
    public static int parseInt(char[] str){
    
    
        int sum = 0;
        for(int i =0;i<str.length;i++){
    
    
            sum += (str[i]-'0')*Math.pow(10,(str.length-1-i));
        }
        return sum;
    }
    public static int parseInt(String str){
    
    
        int sum = 0;
        for(int i =0;i<str.length();i++){
    
    
            sum += (str.charAt(i)-'0')*Math.pow(10,(str.length()-1-i));
        }
        return sum;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37149062/article/details/123770148
今日推荐