1. (30 points) Use BlueJ to write a program for displaying “Hello World!”. 2. (5
ID: 668168 • Letter: 1
Question
1. (30 points) Use BlueJ to write a program for displaying “Hello World!”.
2. (50 points) Modify your Heater class to define three new integer fields: min, max, and increment. The values of min and max should be set by parameters passed to the constructor. The value of increment should be set to 5 in the constructor. Modify the definitions of warmer and cooler so that they use the value of increment rather than an explicit value of 5. Before proceeding further with this exercise, check that everything works as before. Now modify the warmer method so that it will not allow the temperature to be set to a value greater than max.
Similarly modify cooler so that it will not allow temperature to be set to a value less than min. Check that the class works properly. Now add a method, setIncrement, that takes a single integer parameter and uses it to set the value of increment. Once again, test that the class works as you would expect it to by creating some Heater objects within BlueJ. Do things still work as expected if a negative value is passed to the setIncrement method? Add a check to this method to prevent a negative value from being assigned to increment.
3. (20 points) Which of the following expressions return true?
! (4<5)
! false
(2 > 2) || ((4 == 4) && (1 < 0))
(2 > 2) || (4 == 4) && (1 < 0)
(34 != 33) && ! false
Explanation / Answer
1.
public class My
{
private String strtext;
/**
* Constructor for objects of class My
*/
public My()
{
// initialise instance variables
strtext="Hello World";
}
public void sampleMethod()
{
//clears the previous outputs
System.out.println("u000c");
//retrieves the value of the strtext variable and display it on the screen.
System.out.println(strtext);
}
}
2.
public class My
{
int min, max, increment;
/**
* Constructor for objects of class My
*/
public My(int a, int b)
{
min=a;
max=b;
increment=5;
}
public int warmer(int i)
{
if(i<max)
{
return i;
}
else
{
return max;
}
}
public void cooler(int i)
{
if(i>min)
{
increment=i;
}
else
{
return min;
}
}
public void setincrement(int i)
{
increment =i;
}
}
3.
!(4<5) = false
!false= true
(2 > 2) || ((4 == 4) && (1 < 0))=false
(2 > 2) || (4 == 4) && (1 < 0)=false
(34 != 33) && ! false = true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.