• JAVA实践课第二次作业


    JAVA实践课作业 2020/9/22

    第一题

    Write a program that will convert a single temperature in to degrees Centigrade to degrees Fahrenheiht. You should set the value of the temperature into a variable called "centigrade", and it should then print this - together with the converted value to the console. NB to convert from degrees C to degrees F you should multiply by 9, divide by 5 and then add 32.

    写一个程序用于转换温度。

    定义一个叫做“centigrade”的变量,在控制台中打印出它的值和转换后的值。

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            //DecimalFormat df = new DecimalFormat("0.00");
            double centigrade;
            centigrade = in.nextDouble();
            System.out.println("C: " + centigrade);
            System.out.println("F: " + (9 * centigrade / 5 + 32));
        }
    }
    

    第二题

    Write a modified version of the program that you wrote in the previous exercise to convert temperatures in degrees Fahrenheit to degrees Centigrade. Your new program should use a loop to print a temperature conversion table for temperatures from 0 degrees C to 100 degrees C. The output should look something like this:

    ​ Centigrade Fahrenheit

    ​ 0 32

    ​ 10 50

    ​ 20 68

    ​ 30 86

    ​ 40 104

    ​ ...

    修改上一个练习的程序。

    新的程序应该使用循环语句来输出从(0)(100)的摄氏度以及对应的华氏度。

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            //Scanner in = new Scanner(System.in);
            //DecimalFormat df = new DecimalFormat("0.00");
            System.out.println("Centigrade    Fahrenheit");
            for(int centigrade = 1;centigrade <= 100;centigrade++)
                System.out.printf("%-5d          %-5d
    ",centigrade,9 * centigrade / 5 + 32 );
        }
    }
    

    第三题

    1. Write a class called "Temperature" for storing temperature data. It should store temperatures in degrees Centigrade, and have two methods - getCentigrade() and getFahrenheit() which return the temperature in Centigrade and Fahrenheit respectively.

    写一个名为“Temperature”的类用以存放温度数据。

    它应该包含“degree Centigrade”,以及两个方法“getCentigrade()"以及"getFahrenheit()"返回摄氏度和华氏度。

        class Temperature{
            double centigrade;
            double getCentigrade(){
                return centigrade;
            }
            double getFahrenheit(){
                return 9 * centigrade / 5 + 32;
            }
        }
    

    第四题

    1. Write a new program to print a Centigrade-Fahrenheit conversion table, using your "Temperature" class.

    用你的"Temperature"类来打印一份摄氏度-华氏度转换表。

    import java.util.*;
    class Temperature{
        double centigrade;
        double getCentigrade(){
            return centigrade;
        }
        double getFahrenheit(){
            return 9 * centigrade / 5 + 32;
        }
    }
    public class Main {
        public static void main(String[] args) {
            //Scanner in = new Scanner(System.in);
            //DecimalFormat df = new DecimalFormat("0.00");
            Temperature t = new Temperature();
            for(int i = 1;i <= 100;i++){
                t.centigrade = (double)i;
                System.out.println(t.getCentigrade() + "      " + t.getFahrenheit());
            }
        }
    }
    

    第五题

    Using OO design, write a program to print a simple calendar. You should create a Month class, for which the month and the day of the first of the month are supplied as arguments to the constructor. There should be a method to print the month, producing an output similar to that shown below:

    ​ October

    ​ Sun Mon Tue Wed Thu Fri Sat

    ​ 1 2

    ​ 3 4 5 6 7 8 9

    ​ 10 11 12 13 14 15 16

    ​ 17 18 19 20 21 22 23

    ​ 24 25 26 27 28 29 30

    ​ 31

    Your program should prompt the user for the month, and the day of the first of the month, (eg October and Friday in the above example) and it should then print out the entire month

    运用面向对象的思想,设计一个程序实现简单的日历。

    类中包括月份,首天以及相应的构造函数。

    还需要一个方法来输出这张日历。

    import java.math.*;
    import java.io.*;
    import java.text.DecimalFormat;
    import static java.lang.System.out;
    import static java.lang.Math.*;
    import java.util.*;
    class Month{
        String month;
        String theFirstDay;
        int dayOfTheMonth;
        int numberOfTheDay;
        void setMonth(String month1){
            month = month1;
            if(month == "January" || month == "March" || month == "May" || month == "July"
            || month == "August" || month == "October" || month =="December") dayOfTheMonth = 31;
            else if(month == "February") dayOfTheMonth = 28;
            else dayOfTheMonth = 30;
        }
        void setTheFirstDay(String theFirstDay1){
            theFirstDay = theFirstDay1;
            if(theFirstDay == "Sunday") numberOfTheDay = 1;
            else if(theFirstDay == "Monday") numberOfTheDay = 2;
            else if(theFirstDay == "Tuesday") numberOfTheDay = 3;
            else if(theFirstDay == "Wednesday") numberOfTheDay = 4;
            else if(theFirstDay == "Thursday") numberOfTheDay = 5;
            else if(theFirstDay == "Friday") numberOfTheDay = 6;
            else if(theFirstDay == "Saturday") numberOfTheDay = 7;
        }
        void print(){
            System.out.println(month);
            System.out.println("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            int startOfTheDay = 1;
            for(int i = 1;i <= numberOfTheDay;i++)
                System.out.print("    ");
            for(int cureentDay = numberOfTheDay;cureentDay < numberOfTheDay + dayOfTheMonth;cureentDay++){
                System.out.printf("%-5d",startOfTheDay);
                startOfTheDay++;
                if(cureentDay % 7 == 0) System.out.println("");
            }
        }
    }
    public class Main {
        public static void main(String[] args) {
            //Scanner in = new Scanner(System.in);
            //DecimalFormat df = new DecimalFormat("0.00");
            Month mon = new Month();
            mon.setMonth("October");
            mon.setTheFirstDay("Friday");
            mon.print();
        }
    }
    
  • 相关阅读:
    org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON
    小程序用户表wx_user设计
    CSDN支持语法高亮的常用语言(Markdown使用之)
    查看CentOS版本信息
    Java操作MongoDB之mongodb-driver
    使用SpringCache进行缓存数据库查询
    MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommended.
    SpringDataRedis常用方法
    SpringBoot整合Redis进行缓存数据库查询
    java连接neo4j
  • 原文地址:https://www.cnblogs.com/hznumqf/p/13715268.html
Copyright © 2020-2023  润新知