栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实验5面向对象练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(){
        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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781020.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号