Java基础编程题

一、点类

点类5-P156

MyPoint类表示二维平面中的一个点,具有两个double类型属性:

    横坐标

    纵坐标

并具有一个构造方法(与类同名的方法)和以下普通方法:

       1  构造方法:接收两个double型数据作为参数,分别设定为横坐标和纵坐标。

       2  构造方法:接收一个MyPoint类型对象,分别设定为相同的横坐标和纵坐标。

       3 display()方法,无参数,输出坐标信息,格式形如“(10.0,20.0)”。

       4 getInfo()方法,无参数,返回字符串类型的坐标信息,格式形如"(10.0,20.0)"。

5所有属性的置取方法。

6 updatePoint()方法,接收两个double型数据作为参数,分别设置为当前点的横纵坐标。

7 updatePoint()方法,接收一个MyPoint类型对象作为参数,将当前点坐标更新为参数的坐标值。

8 getDistance()方法,接收一个MyPoint类型对象作为参数,计算并返回double类型的两点间距离值。

9 getDistance()方法,接收两个double型数据参数,作为另一点横纵坐标,计算并返回double类型的两点间距离值。

要求编程实现上述类,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test{

    public static void main(String[] args)     {

MyPoint p1 = new MyPoint(40,0);

MyPoint p2 = new MyPoint(0,30);

MyPoint p3 = new MyPoint(0,0);

int x1=20,y1=15;

showPoint(p1,p2);

System.out.println("distance: "+p1.getDistance(p2));

p1.updatePoint(x1,y1);

p2.updatePoint(p3);

showPoint(p1,p2);

System.out.println("distance: "+p2.getDistance(x1,y1));

    }

private static void showPoint(MyPoint p1,MyPoint p2){

System.out.println("--------------");

System.out.println("p1: "+p1.getInfo());

System.out.println("p2: "+p2.getInfo());

}

}

【输入形式】
【输出形式】

--------------

p1: (40.0,0.0)

p2: (0.0,30.0)

distance: 50.0

--------------

p1: (20.0,15.0)

p2: (0.0,0.0)

distance: 25.0

import java.math.*;
public class Test
{

    public static void main(String[] args)     {

        MyPoint1 p1 = new MyPoint1(40,0);

        MyPoint1 p2 = new MyPoint1(0,30);

        MyPoint1 p3 = new MyPoint1(0,0);

        int x1=20,y1=15;



        showPoint(p1,p2);

        System.out.println("distance: "+p1.getDistance(p2));



        p1.updatePoint(x1,y1);

        p2.updatePoint(p3);

        showPoint(p1,p2);

        System.out.println("distance: "+p2.getDistance(x1,y1));

    }

    private static void showPoint(MyPoint1 p1,MyPoint1 p2){

        System.out.println("--------------");

        System.out.println("p1: "+p1.getInfo());

        System.out.println("p2: "+p2.getInfo());

    }

}
class MyPoint1{
    private double x;
    private double y;
    public MyPoint1(double x,double y){
        this.x=x;
        this.y=y;
    }
    public MyPoint1(MyPoint1 point){
        this.x=point.x;
        this.y=point.y;
    }
    public void display(){
        System.out.println("("+x+","+y+")");
    }
    public String getInfo(){
        return "("+x+","+y+")";
    }
    public void updatePoint(double x,double y){
        this.x=x;
        this.y=y;
    }
    public void updatePoint(MyPoint1 point){
        this.x=point.x;
        this.y=point.y;
    }
    public double getDistance(double x,double y){
//        double result1=Math.pow(x,2);
//        double result2=Math.pow(y,2);
        return Math.sqrt(Math.pow((x - this.x), 2) + Math.pow((y - this.y), 2));
    }
    public double getDistance(MyPoint1 point){
        return Math.sqrt(Math.pow((point.x - this.x), 2) + Math.pow((point.y - this.y), 2));
    }


}

二、数学函数计算-方法重载

已知公式如下图,编程求f(x,y,z)的值 ,在Test类中实现x,y,z值 的输入及结果的输出 。

编写MyMath类,实现静态的4个重载方法f(),分别计算不同的函数值,使给定的Test类能正常运行,并实现指定的输出内容。

import java.util.Scanner;

public class Test {

    public static void main(String[] args){

        int x, y, z;

        Scanner in = new Scanner(System.in);

        x = in.nextInt();

        y = in.nextInt();

        z = in.nextInt();

        in.close();

        int result;

        if (x < 0) {

            result = MyMath.f();

        } else if (x >= 0 && y < 0) {

            result = MyMath.f(x);

        } else if (x >= 0 && y >= 0 && z < 0) {

            result = MyMath.f(x, y);

        } else {

            result = MyMath.f(x, y, z);

        }

        System.out.println(result);

    }

}

【输入形式】-1 2 5
【输出形式】0

import java.util.Scanner;

public class Test {

    public static void main(String[] args){

        int x, y, z;

        Scanner in = new Scanner(System.in);

        x = in.nextInt();

        y = in.nextInt();

        z = in.nextInt();

        in.close();



        int result;

        if (x < 0) {

            result = MyMth.f();

        } else if (x >= 0 && y < 0) {

            result = MyMth.f(x);

        } else if (x >= 0 && y >= 0 && z < 0) {

            result = MyMth.f(x, y);

        } else {

            result = MyMth.f(x, y, z);

        }

        System.out.println(result);

    }

}
class MyMth{
    public static int f(){
        return 0;
    }
    public static int f(int x){
        return x*x;
    }
    public static int f(int x, int y){
        return x*x+y*y;
    }
    public static int f(int x,int y,int z){
        return x*x+y*y+z*z;
    }
}

三、钱类-方法重载和staic

编写Money类,要求具有yuan, jiao, fen三个int类型的属性及相应的置取方法,所表示的金额分别按元角分保存在各个属性中。

另外 ,还具有以下方法:

1 具有重载的四个set()方法,具体要求如下:

(1)参数为int类型,将参数值存入yuan, jiao和fen都置为0;

(2)参数为double类型,将参数值按分做四舍五入,然后分别存入对应的属性;

(3)参数为字符串String,对字符串中的数字做解析后,按分做四舍五入,将金额分别存入对应的属性;

(4)参数为Money类的对象,将参数中的金额分别存入对应的属性。

2 有两个可实现金额计算的方法

(1) times(int n)方法,参数为int,返回值为Money类对象,其中的总金额为当前对象的总金额乘以参数n

(2) add(Money money)方法,参数为Money类对象,返回值为Money类对象,其中的总金额为当前对象的总金额加上参数money中的总金额。

3 有一个静态方法,按照指定格式输出总金额

writeOut(String owner, Money money)方法,输出格式如“owner have/has XX Yuan XX Jiao XX Fen.”的字符串,所输出的的金额是参数money中的总金额。

字符串转浮点数可以使用静态方法:Double.parseDouble(String)。

要求编程实现类Money,使给定的Test类能正常运行,并实现指定的输出内容。

public class Test{

       public static void main(String[] args){

              Money myMoney = new Money();

              Money yourMoney = new Money();  

              Money hisMoney = new Money();

              Money herMoney = new Money();

             

              int amountInt=365;

              double amountDouble=254.686;

              String amountString="368.244";

              myMoney.set(amountInt);

              Money.writeOut("I",myMoney);

              yourMoney.set(amountDouble);

              Money.writeOut("You",yourMoney);

              hisMoney.set(amountString);

              Money.writeOut("He",hisMoney);

              herMoney.set(myMoney);

              Money.writeOut("She",herMoney);

              herMoney = yourMoney.times(3);

              Money.writeOut("She",herMoney);

              herMoney = yourMoney.add(hisMoney);

              Money.writeOut("She",herMoney);

              System.out.println("Remember: A penny saved is a penny earned.");

       }

}

import java.math.*;

public class Test{

    public static void main(String[] args){

        Money myMoney = new Money();

        Money yourMoney = new Money();

        Money hisMoney = new Money();

        Money herMoney = new Money();



        int amountInt=365;

        double amountDouble=254.686;

        String amountString="368.244";



        myMoney.set(amountInt);

        Money.writeOut("I",myMoney);



        yourMoney.set(amountDouble);

        Money.writeOut("You",yourMoney);



        hisMoney.set(amountString);

        Money.writeOut("He",hisMoney);



        herMoney.set(myMoney);

        Money.writeOut("She",herMoney);



        herMoney = yourMoney.times(3);

        Money.writeOut("She",herMoney);



        herMoney = yourMoney.add(hisMoney);

        Money.writeOut("She",herMoney);



        System.out.println("Remember: A penny saved is a penny earned.");

    }

}
class Money{
    private int yuan;
    private int jiao;
    private int fen;
    public Money(){
        this.yuan=0;
        this.jiao=0;
        this.fen=0;
    }
    public void setYuan(int yuan) {
        this.yuan = yuan;
    }

    public int getYuan() {
        return yuan;
    }

    // jiao的setter和getter方法
    public void setJiao(int jiao) {
        this.jiao = jiao;
    }

    public int getJiao() {
        return jiao;
    }

    // fen的setter和getter方法
    public void setFen(int fen) {
        this.fen = fen;
    }

    public int getFen() {
        return fen;
    }
    public void set(int amount){
        this.yuan=amount;
        this.jiao=0;
        this.fen=0;
    }
    public void set(double amount){
        long totalFen=Math.round(amount*100);
        this.yuan=(int)(totalFen/100);
        totalFen %=100;
        this.jiao=(int)(totalFen/10);
        this.fen=(int)(totalFen%10);
    }
    public void set(String amount){
        double parseAmount=Double.parseDouble(amount);
        set(parseAmount);
    }
    public void set(Money money){
        this.yuan=money.yuan;
        this.jiao= money.jiao;
        this.fen= money.fen;
    }
    public Money times(int n){
        Money result=new Money();
        long totalFen=(long) this.yuan*100+this.jiao*10+this.fen;
        totalFen *=n;
        result.yuan =(int)(totalFen /100);
        totalFen %=100;
        result.jiao =(int)(totalFen/10);
        result.fen=(int)(totalFen%10);
        return result;
    }
    public Money add(Money money){
        Money result=new Money();
        long totalFen1=(long) this.yuan*100+this.jiao*10+this.fen;
        long totalFen2 =(long) money.getYuan()*100+money.getJiao()*10+ money.getFen();
        long total = totalFen1+totalFen2;
        result.yuan=(int) (total /100);
        total %=100;
        result.jiao=(int) (total / 10);
        result.fen = (int)(total %10);
        return result;

    }
    public static void writeOut(String owner , Money money){
        System.out.println(owner + " have/has " + money.yuan + " Yuan " + money.jiao + " Jiao " + money.fen + " Fen.");
    }
}

希望大家多多关注,多多点赞,祝大家心想事成,更上一层楼。

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
写出一个People,并由该做基派生出子Employee和Teacher。其中People具有name、age两个保护成员变量,分别为String型和整型,且具有共有的getAge()成员方法,用于返回age变量的值,并编写一个两个参数的构造函数。Employee具有保护成员String变量employeeNo,Teacher有String型的teano和zc变量,并分别为两个子编写一个无参的构造函数。若使两个子正常的编译,请在父中解决该问题。 public class Employee extends People{ protected String employeeNo ; //construct method Employee() { } } package Exercise1; public class People { protected String name ; protected int age ; //construct method People() { } People(String name,int age) { this.name = name ; this.age = age ; } //getAge public int getAge() { return this.age ; } } package Exercise1; public class Teacher extends People{ protected String teano ; protected String zc ; //construct method Teacher() { } } package Exercise1; public class MainTest { public static void main(String[] args) { // TODO Auto-generated method stub Employee em = new Employee(); em.age = 20; em.employeeNo = "1234560"; em.name = "Jim" ; System.out.println("Employee : name :"+em.name+"age:"+em.age+"emplyeeNo:"+em.employeeNo); Teacher tea = new Teacher(); tea.age = 26; tea.name = "Tom" ; tea.teano= "456"; tea.zc = "Go" ; System.out.println("Teacher : name :"+tea.name+"age:"+tea.age+"teano:"+tea.teano+"zc:"+tea.zc); } } 题目:判断101-200之间有多少个素数,并输出所有素数。 public class SuShu { public static void main(String[] args) { int m; int i; int k=0; for(m=1;m<=100;m++) { for(i=2;i<m;i++) { if((m%i)==0) { break; } } if(i==m) { k++; System.out.println(m); } } System.out.println("sushu you"+k); } } 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 import javax.swing.JOptionPane; public class Exer3 { public static void main(String[] args) { String s1 = JOptionPane.showInputDialog(" Input a string (s1>=10000 && s1<100000) :"); String s2 = new StringBuffer(s1).reverse().toString(); if(s1.equals(s2)){ JOptionPane.showMessageDialog(null,"yes"); }else{ JOptionPane.showMessageDialog(null,"no"); } } } 题目:打印出杨辉三角形(要求打印出10行 import java.util.Scanner; public class YHSanJiao { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a data:"); int n = input.nextInt(); int[][] a = new int[n][n]; for(int i = 0; i<n ; i++){ for(int j = 0; j <=i;j++){ if(j==0||i==j) a[i][j]=1; else a[i][j] = a[i-1][j-1] + a[i-1][j]; System.out.print(a[i][j] + " "); } System.out.println(); } } } Enter a data:10 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 GradeDemo中的checkAnswer方法实现不定项选择判分,具体要求如下: 1) 考生答案和正确答案相同(不考虑选项顺序),得5分。 2) 考生答案不全,得2分。 3) 考生答案中有错误答案,得0分。 部分示例如下: 正确答案 考生答案 得分 ABC ABC/ACB/BAC/CBA/BCA 5 ABC A/B/C/AB/AC/BC/CB/CA/BA 2 ABC D/AD/BD/CD/ABD/BCD/ACD/ABCD 0 请完成checkAnswer方法,根据正确答案和考生答案,返回得分。 String的相关方法: public char charAt(int index):返回指定索引处的 char 值 public boolean equals(Object anObject):比较此字符串与指定的对象。 public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引, 找不到返回-1 public String substring(int beginIndex, int endIndex):返回一个新的字符串,该 子字符串始于指定索引处的字符,一直到此字符串末尾。 public class GradeDemo { public static void main(String[] args) { String correctAnswer = "ACD"; String userAnswer = "AD"; int grade = checkAnswer(correctAnswer,userAnswer); System.out.println("本题的得分是 : " + grade); } public static int checkAnswer(String correctAnswer,String userAnswer){} public static boolean checkString(String correctAnswer,String userAnswer){} } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import javax.swing.JOptionPane; public class CheckAnswer { public static void main(String[] args) { String correctAnswer = "ACD"; String userAnswer = JOptionPane.showInputDialog("考生答案为:"); userAnswer = userAnswer.toUpperCase(); int grade = checkAnswer(correctAnswer,userAnswer); System.out.println("本题的得分是: " + grade); } public static int checkAnswer (String correctAnswer,String userAnswer ){ if(correctAnswer.length()==userAnswer.length()){ if(checkString(correctAnswer,userAnswer)) return 5; else return 0; } else if(correctAnswer.length()>userAnswer.length()){ if(checkString(correctAnswer,userAnswer)) return 2; else return 0; } else return 0; } public static boolean checkString (String correctAnswer,String userAnswer ){ boolean m = true; for(int i = 0; i < userAnswer.length(); ++i){ if(correctAnswer.indexOf(userAnswer.charAt(i))==-1){ m= false; break; } } return m ; } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值