Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 7b: Extending the Temperature Class 
 
Introduction: 
 
 
As you saw in the previous lab, classes in Java are used to create abstract data types (ADTs) by 
combining data and operations that logically belong together. The "private" section of the class is 
where we normally put attributes (variables) that we wish to hide from the user. The "public" 
section of the class is normally where we put the methods (functions) that we want the users to 
call when they want to manipulate the data. 
 
Incremental software development is one of the major themes of this class, so in this lab we will 
focus on how to extend and test an existing class. We will be using the "Temperature" class you 
worked on in the previous lab. Your task in this lab is to add several useful methods to the class 
and then use them in a small main program. 
 
Instructions: 
 
Consider the following Java program. The Temperature class includes a collection of public 
methods and private attributes. The Main class has a main program that prompts the user for a 
start and end temperature and prints a small Fahrenheit to Celsius temperature conversion table. 
 
public class Temperature 
{ 
    // Private variables 
    private double CelsiusTemperature; 
    private static final double ABSOLUTE_ZERO = -273.15; 
    private static final double SUN_CORE_TEMP = 15710000.0; 
     
    // Constructors 
    public Temperature() 
    { 
        CelsiusTemperature = 0; 
    } 
     
    public double getCelsius() 
    { 
        return CelsiusTemperature; 
    } 
     
    public double getFahrenheit() 
    { 
        return 9.0 * CelsiusTemperature / 5.0 + 32.0; 
    } 
     
    public void setCelsius(double Temp) 
    { 
        CelsiusTemperature = Temp; 
        if (CelsiusTemperature < ABSOLUTE_ZERO) 
            CelsiusTemperature = ABSOLUTE_ZERO; 
        if (CelsiusTemperature > SUN_CORE_TEMP) 
            CelsiusTemperature = SUN_CORE_TEMP; 
    } 
     
    public void setFahrenheit(double Temp) 
    { 
        CelsiusTemperature = (Temp - 32.0) * 5.0 / 9.0 ; 
        if (CelsiusTemperature < ABSOLUTE_ZERO) 
            CelsiusTemperature = ABSOLUTE_ZERO; 
        if (CelsiusTemperature > SUN_CORE_TEMP) 
            CelsiusTemperature = SUN_CORE_TEMP; 
    } 
} 
 
import java.util.Scanner; 
public class Main 
{ 
   public static void main(String[] args)  
   { 
      // Get user input 
      Scanner scnr = new Scanner(System.in); 
      System.out.print("Enter start temperature: "); 
      double Start = scnr.nextDouble(); 
      System.out.print("Enter end temperature: "); 
      double End = scnr.nextDouble(); 
 
      // Loop printing temperatures 
      for (double F = Start; F <= End; F++) 
      { 
         Temperature Temp = new Temperature(); 
         Temp.setFahrenheit(F); 
         System.out.printf("%3.1fF = %3.1fC\n",  
            F, Temp.getCelsius()); 
      } 
   } 
} 
 
Step 1: Copy the code above into "Temperature.java" and "Main.java" in your Java program 
editor, and compile it. Hopefully, you will not get any error messages. Run the program and type 
in "75 85" to see what is printed out. You should see a table of F to C temperature conversions. 
 
Step 2: In general, it is helpful to have a "print" method in every class so users can more easily 
output the values stored in the object. Edit Temperature.java to add a new public method called 
"print" with no parameters. Add code to this method so the two temperatures are printed in the 
same format as the current main program (e.g. "80.0 F = 26.7 C").  
Remember, when you are inside the implementation of the "print" method, you can access any 
private variable (e.g. CelsiusTemperature). You can also call any Temperature method without 
adding an object name before the method call (e.g. getFahrenheit()). 
 
Step 3: Compile and run your program to verify that the print method is syntactically correct. 
Now we can call the "print" method in your main program instead of using System.out.printf. 
Edit your code to make these changes, and recompile the program. When you run it, the output 
should be the same as before, but the main program should be smaller and slightly cleaner 
looking. 
 
Step 4: Since we have a way to output temperatures, lets add a method to input temperatures in 
either Fahrenheit or Celsius. Edit your program and add a public "read" method to the 
Temperature class. Now add code to read a number followed by a single character. Remember, 
there is no "nextChar" method in the scanner class, so you will have to read the letter into a 
String str and check the first character using "str.charAt(0)". If the user enters "30 c" or 30 C", 
you should store this as 30 Celsius. If the user enters "80 f" or "80 F", you should store this as 80 
Fahrenheit. If the user enters any other character besides c, C, f, F you should "fail quietly" and 
simply ignore their input. 
 
Step 5: Compile and run your program to verify that the read method is syntactically correct. 
Now it is time to modify the main program to use your new read method. First, go to the main 
program and declare two Temperature objects called "Start" and "End" (and remove the double 
variables with these names). Next, remove the current input statements and use the "read" 
method to initialize your two temperature values. 
 
Step 6: If you try to compile at this point, you will get syntax errors. To fix your program, you 
must modify the first line of the for loop. Think about what we are trying to do. We want the 
program to loop over a range of Fahrenheit temperatures. How can you do this using the Start 
and End objects, and the methods in the Temperature class? 
 
Step 7: Once you have your code compiling, run the program and test it by typing in "75 F 85 F" 
to verify that you are correctly reading temperature values. You should get the same output as 
your initial program. What happens if you enter two Celsius values? Finally, see if you can find 
an input that will cause the program to crash or produce a strange output. If you find something, 
you should add a comment in the code explaining that "the following input will cause an error". 
 
Step 8: When your program is working correctly, upload your final program and a copy of your 
program output into Blackboard for grading.