《面向对象Java编程》上机作业(软外)

由于篇幅问题,8次上机仅公开展示第1次上机题目。有文件需要请根据链接自取https://pan.baidu.com/s/1yV2xh5aj_lnVZzLs9hYoyA?pwd=45g6 
提取码:45g6 

1. 编译器的试验

本题需要用到HelloWorld程序(代码参考课件),需要在HelloWorld程序中引入一些变动(错误)并重新编译。尝试一下的改动,每次改一项(进行下一项时,前一项改动需要恢复),修改、重新编译并观察编译结果。如有编译错误,则需要理解编译提示并与之前的改动关联起来。通过这个操作,能够促进对编译信息的理解。以下为需要修改的内容列表:

  • delete the “}” at the end of the file
  • misspell “main” as “man
  • misspell “HelloWorld” as “helloworld
  • delete the “;
  • delete the closing “"
  • misspell “args” as “double

理解并熟悉错误信息在编程过程中是很重要的能力。当然需要注意的是,有些字面含义不一定会有直接的帮助——提示信息中的错误位置很可能不是真正错误的地方。因此需要思考并进行尝试。

  • 问题分析

  • 代码实现(按每个改动,分别提供)

1.public class HelloWorld{

public static void main(String argv[]){

System.out.println("Hello World!");

}

2.public class HelloWorld{

public static void man(String argv[]){

System.out.println("Hello World!");

}

}

3.public class helloworld{

public static void main(String argv[]){

System.out.println("Hello World!");

}

4.public class HelloWorld{

public static void main(String argv[]){

System.out.println("Hello World!")

}

}

5.public class HelloWorld{

public static void main(String argv[]){

System.out.println(Hello World!);

}

}

6.public class HelloWorld{

public static void main(String double[]){

System.out.println("Hello World!");

}

}

  • 运行结果(按每个改动,分别提供)
  1. 解析时已到达文件结尾
  2. 未报错
  3.  类helloworld是公共的, 应在名为 helloworld.java 的文件中声明

public class helloworld{

4.需要';'

5.HelloWorld.java:3: 错误: 需要')'

                System.out.println(Hello World!);

                                        ^

HelloWorld.java:3: 错误: 非法的表达式开始

                System.out.println(Hello World!);

                                               ^

HelloWorld.java:3: 错误: 不是语句

                System.out.println(Hello World!);

                                              ^

HelloWorld.java:3: 错误: 非法字符: '\uff1b'

                System.out.println(Hello World!);

                                                ^

HelloWorld.java:3: 错误: 需要';'

                System.out.println(Hello World!);

                                                 ^

HelloWorld.java:5: 错误: 解析时已到达文件结尾

6.HelloWorld.java:2: 错误: 需要<标识符>

        public static void main(String double[]){

                                      ^

HelloWorld.java:2: 错误: 需要';'

        public static void main(String double[]){

                                             ^

HelloWorld.java:2: 错误: 非法的类型开始

        public static void main(String double[]){

                                              ^

HelloWorld.java:2: 错误: 需要<标识符>

        public static void main(String double[]){

                                               ^

HelloWorld.java:2: 错误: 需要';'

        public static void main(String double[]){

                                                ^

HelloWorld.java:3: 错误: 需要<标识符>

                System.out.println("Hello World!");

                                  ^

HelloWorld.java:3: 错误: 非法的类型开始

                System.out.println("Hello World!");

                                   ^

HelloWorld.java:3: 错误: 非法字符: '\uff1b'

                System.out.println("Hello World!");

                                                  ^

HelloWorld.java:5: 错误: 需要class, interface或enum

  • 思考及总结

不同的错误会导致不同的影响

对于语言的学习,要重视语法

有些错误即使存在,不影响程序的编译以及结果

2. 写一个程序叫CheckMark,该程序会对一个变量mark进行判断,如果大于等于50,则在控制台输出PASS,否则输出FAIL。提示:mark的值在main方法中进行设定。

  • 问题分析

输入时,将字符串转换成数字类型并进行判断

  • 代码实现

public class CheckMark {
    public static void main(String[] args){
        int mark=Integer.parseInt(args[0]);
        if (mark>=50){
            System.out.println("PASS");}
        else{
            System.out.println("FAIL");
        }
    }
}

  • 运行结果

  • 思考及总结

输入要紧跟在运行后面

如应该输入 java CheckMark 89

3. 写一个程序叫CozaLozaWoza,该程序输出从1到110的所有整数,输出时每行显示11个数字,并且若数字是3的倍数则输出“Coza”代替该数字的位置,若是5的倍数则输出“Loza”,7的倍数则为“Woza”,同时是3和5的倍数则为“CozaLoza”……以此类推。输出样例如下:

  • 问题分析

对每个数分别做3/5/7判断,倘若能被3整除,则将该数除以三之后进行是否被5整除的判断

  • 代码实现

 public class CozaLozaWoza {
    public static void main(String args[]){
        int i,j;
        for(i=1;i<111;i++){
            j=i;
            if(j%3==0){
                System.out.print("Coza");
                j=j/3;
            }
            if(j%5==0){
                System.out.print("Loza");
                j=j/5;
            }
            if(j%7==0){
                System.out.print("Woza");
                j=j/7;
            }
            if(i==j){
                System.out.print(i);
            }
            System.out.print(" ");
            if(i%11==0){
                System.out.println();
            }
        }
    }
}

  • 运结果

  • 思考及总结

要区分println和print方法的区别,println使用后会默认切换到下一行

4. 以下是一个货币转换的算法,主要是从人民币(CNY)转换为俄罗斯卢布(RUB),兑率是18.58。

兑换的算法如下:

• declare a final double variable for the RUB exchange rate and initialise it to 18.58

• declare a double variable for the CNY amount to be provided

• declare a double variable for the RUB amount to be calculated

• declare and create a Scanner object and initialise it

• ask the user how much money in CNY they wish to exchange

• obtain this amount from them using sc.nextDouble() and store the result in the relevant variable

• calculate how many RUB this is worth by multiplying by the exchange rate and store the result in the relevant variable and

• tell the user how many RUB they would receive for the amount of CNY they indicated. 

请编写一个程序CurrencyConverter,实现上述算法:提示用户输入一个数,代表CNY的量,将其转换为RUB并输出转换后的金额。

  • 问题分析

导入scan模块实现用户输入,用final定义常量

  • 代码实现

import java.util.Scanner;
public class CurrencyConverter {
    public static void main(String args[]){
        final double RATE=18.58;
        double CNY,RUB;
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入数据");
        CNY=scan.nextDouble();
        RUB=CNY*RATE;
        System.out.println("转换后的金额为:"+RUB);
    }
}

  • 运行结果

  • 思考及总结

Java中用户输入相较于其他语言略微繁琐,需要反复练习

5. 打印出100-1000范围内所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数",因为153 = 1的三次方+5的三次方+3的三次方。

  • 问题分析

需要将每个三位数的个十百位分别取到

  • 代码实现

public class exercise01 {
    public static void main(String[] args) {
        int i, j;
        for (i = 100; i < 1000; i++) {
            if (i == (int) (Math.pow(i / 100, 3) + Math.pow(i % 100 / 10, 3) + Math.pow(i % 10, 3))) {
                System.out.println(i);
            }
        }
    }
}

  • 运行结果

  • 思考及总结

使用了pow函数之后在前面需要加强制类型转换

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Enza、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值