JAVA期末速成库(6)第六章

一、习题介绍

第六章

Check Point:P211 6.4,6.5,6.11,6.15,6.18

Programming Exercise:6.5,6.8

二、习题及答案

Check Point:

6.4

True or false? A call to a method with a void return type is always a statement itself, but a call to a value-returning method cannot be a statement by itself.

6.对还是错?调用返回类型为void的方法本身总是一个语句,但是对返回值方法的调用本身不能是语句。

答:错误。调用返回类型为void的方法本身可以是一个语句,同样,对返回值方法的调用本身也可以是语句。关键在于调用是否产生了一个需要的值或者是一个独立的操作。例如,一个void方法可以被单独调用作为一个语句,而一个返回值的方法调用可以被用于赋值或者作为另一个表达式的一部分。

6.5

What is the return type of a main method?

6.5主方法的返回类型是什么?

答:主方法(main method)的返回类型是 void 。

6.11

How is an argument passed to a method? Can the argument have the same name as its parameter?

6.11参数是如何传递给方法的?实参能否与其形参同名?

答:参数是通过值传递(pass by value)给方法的,这意味着方法接收的是实际参数值的一个副本。实参(actual parameters)可以与形参(formal parameters)同名,这不会影响参数传递,因为方法内部使用的是形参的名称。

6.15

What is method overloading? Is it permissible to define two methods that have the same name but different parameter types? Is it permissible to define two methods in a class that have identical method names and parameter lists but different return value types or different modifiers?

6.15什么是方法重载?是否允许定义两个具有名称相同但参数类型不同?是否允许在一个类中定义两个方法,它们具有相同的方法名称和参数列表,但返回值类型或修饰符不同?

答:方法重载(method overloading)是指在同一个类中定义多个同名方法,但它们的参数列表(参数的数量、类型或顺序)不同,这是允许的。然而,仅仅改变返回值类型或方法的修饰符(如访问修饰符)是不足以构成方法重载的,因为编译器是根据整个参数列表(包括类型和数量)来识别方法的。

6.18

What is a local variable?

6.18什么是局部变量?

答:局部变量是在方法内部声明的变量,它们只在该方法的作用域内可见和可用。一旦方法执行完毕,局部变量就会被销毁。

Programming Exercise:

* 6.5

(对三个数字排序)用下面的标题写一个方法来显示三个

数字:按递增顺序排列的数字:

displaySortedNumbers()

双num1,双num2,双num3)

VideoNote

逆向积分编程练习235

编写一个测试程序,提示用户输入三个数字并调用

方法以递增顺序显示它们。

import java.util.Scanner;



public class NumberSorter {



    // 显示按递增顺序排列的三个数字

    public static void displaySortedNumbers(double num1, double num2, double num3) {

        // 使用简单的选择排序对三个数字进行排序

        double temp;

        if (num1 > num2) {

            temp = num1;

            num1 = num2;

            num2 = temp;

        }

        if (num2 > num3) {

            temp = num2;

            num2 = num3;

            num3 = temp;

        }

        if (num1 > num2) {

            temp = num1;

            num1 = num2;

            num2 = temp;

        }



        // 打印排序后的数字

        System.out.println("按递增顺序排列的数字是: " + num1 + ", " + num2 + ", " + num3);

    }



    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);



        // 提示用户输入三个数字

        System.out.print("请输入第一个数字: ");

        double firstNumber = scanner.nextDouble();



        System.out.print("请输入第二个数字: ");

        double secondNumber = scanner.nextDouble();



        System.out.print("请输入第三个数字: ");

        double thirdNumber = scanner.nextDouble();



        // 调用方法显示排序后的数字

        displaySortedNumbers(firstNumber, secondNumber, thirdNumber);



        scanner.close();

    }

}

运行结果: 

*6.8

(Conversions between Celsius and Fahrenheit) Write a class that contains the fol lowing two methods:
/** Convert from Celsius to Fahrenheit */
public static double celsiusToFahrenheit(double celsius)
/** Convert from Fahrenheit to Celsius */
public static double fahrenheitToCelsius(double fahrenheit)
The formula for the conversion is:
fahrenheit = (9.0 / 5) * celsius + 32
celsius = (5.0 / 9) * (fahrenheit – 32)
Write a test program that invokes these methods to display the following tables:

*6.8(摄氏温度和华氏温度之间的转换)编写一个包含以下两个方法的类:
/**从摄氏转换到华氏*/
公共静态双摄氏(双摄氏度)
/**将华氏温度转换为摄氏温度*/
公共静态双华氏度
换算公式为:
华氏温度=(9.0 / 5)*摄氏温度+ 32
摄氏=(5.0 / 9)*(华氏- 32)
编写一个测试程序,调用这些方法来显示以下表格:

public class TemperatureConverter {
    // Method to convert Celsius to Fahrenheit
    public static double celsiusToFahrenheit(double celsius) {
        return (celsius * 9.0 / 5) + 32;
    }

    // Method to convert Fahrenheit to Celsius
    public static double fahrenheitToCelsius(double fahrenheit) {
        return (fahrenheit - 32) * 5.0 / 9;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Celsius\t\tFahrenheit\t| Fahrenheit\tCelsius");
        for (int i = 40; i >= 30; i--) {
            double fahrenheit = TemperatureConverter.celsiusToFahrenheit(i);
            System.out.printf("%d.0\t%.1f\t|\t%.1f\t%6.2f%n", i, fahrenheit, 120.0, TemperatureConverter.fahrenheitToCelsius(120.0));
        }
    }
}

运行结果: 

 结语

多一分坚持

就会少一分遗憾  

!!!

  • 18
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT 青年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值