Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 7a: Defining and Using the Temperature Class 
 
Introduction: 
 
There are two widely used programming methods today: procedural programming and object 
oriented programming. In a procedural program, you typically have data stored in variables and 
arrays, and a collection of functions that perform operations on this data. The data and functions 
are separate, and we can mix and match them any way we wish. This is what we have been doing 
so far this semester. 
 
In object oriented programming, the goal is to combine data and operations that belong together 
to create "objects". By doing this, we can use the syntax of the programming language to tie the 
data and operations to each other, and to "hide" the representation of the data and the 
implementation of the operations from users of these objects. This reduces confusion caused by 
too much mixing and matching, and makes it easier for programmers to write larger and more 
complex programs. 
 
In Java we define "classes" to specify the data and operations that go together in objects. The 
data contained in an object are known as the object's "attributes", and the functions that 
implement operations are known as "methods". Java uses the keywords “public” and “private” to 
tell users of the class which attributes and methods can be accessed in other programs, and which 
are hidden and unavailable for use. 
 
In this lab we will be defining and using the "Temperature" class. As you will see, this class is 
used to store and manipulate temperatures in either the Fahrenheit or Celsius scale. By putting 
the temperature conversion code inside the class implementation, users of this class don't need to 
worry about conversion formulas or error checking. 
 
Instructions: 
 
Consider the following Java program that creates and uses two Temperature objects.  
 
public class Main 
{ 
   public static void main(String[] args)  
   { 
Temperature freezing = new Temperature(); 
Temperature boiling = new Temperature(); 
freezing.setCelsius(0); 
boiling.setCelsius(100); 
System.out.println("freezing: " + freezing.getFahrenheit()+"F"); 
System.out.println("boiling:  " + boiling.getFahrenheit()+"F"); 
   } 
} 
 
Step 1: Copy this Java program into OnlineGDB and compile it. You will see several error 
messages saying, “cannot find symbol”. This is because “Temperature” is a user-defined class, 
and we have not provided the implementation of this class. 
 
Step 2: In order to make the compiler errors go away, you need to create a new file called 
“Temperature.java” and copy the following code into this file. You can probably tell in advance 
that this code is incorrect, but we want to get something simple to compile before going on. 
 
public class Temperature 
{ 
    private double CelsiusTemperature; 
 
    public Temperature() 
    { 
        CelsiusTemperature = 0; 
    } 
     
    public double getCelsius() 
    { 
        return CelsiusTemperature; 
    } 
     
    public double getFahrenheit() 
    { 
        return CelsiusTemperature; 
    } 
     
    public void setCelsius(double Temp) 
    { 
        CelsiusTemperature = Temp; 
    } 
     
    public void setFahrenheit(double Temp) 
    { 
        CelsiusTemperature = Temp; 
    } 
} 
 
Step 3: When you run this program you will see that it prints incorrect values. The correct 
freezing and boiling points for water are 32F and 212F. Edit the “getFahrenheit” method to say 
“return 9.0 * CelsiusTemperature / 5.0 + 32.0”.  Then edit the “setFahrenheit” method to say 
“CelsiusTemperature = (Temp - 32.0) * 5.0 / 9.0”. Now your program should run correctly. 
 
Step 4: We all know that absolute zero is the coldest temperature possible, but the current code 
is not checking for this yet. Go to the top of the Temperature class and add the following line to 
define a constant for absolute zero. 
private static final double ABSOLUTE_ZERO = -273.15; 
 
Step 5: Edit the two “set” methods to add a test to see if the temperature being saved is less than 
absolute zero. If so, you should save absolute zero instead. To test this error-checking feature, 
edit your main program to declare a new Temperature object called "coldcold" after the other 
Temperature objects. Call the "setCelsius" method to initialize this object to -500 Celsius. Then 
print “coldcold” in Celsius after the freezing and boiling.  
 
Step 6: There is still some debate on the hottest temperature possible. For the purposes of this 
program, lets use the temperature at the center of our sun, since it is the hottest thing around. Edit 
your class interface to add a constant "SUN_CORE_TEMP" equal to 15,710,000 Celsius. Then 
edit your two "set" methods to add some error checking code to make sure that we do not save a 
temperature larger than this value. Once you have this code written, compile and run your 
program to make sure it is still working correctly. 
 
Step 7: Finally, modify the main program to declare a new Temperature object called "hothot" 
after the "coldcold" object. Call the "setCelsius" method to initialize this object to 20,000,000 
Celsius. Then, print “hothot” in Celsius just after the other prints. Compile and run your program 
to see what is output this time. Hopefully your value of "hothot" will not be 20,000,000 Celsius. 
 
Step 8: When your program is working correctly, upload your final program and a copy of your 
program output into Blackboard for grading.