ITP 120 - MyProgrammingLab Assignments - Chapter 4

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Write the definitions of two classes Day and Night. Both classes have no constructors , methods or instance variables . Note: For this exercise, please do not declare your classes using the public visibility modifier.

class Day{} class Night{}

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, setting the desired temperature, and reporting the previously set temperature. The following methods provide this behavior : turnOn and turnOff, setTemp, which accepts an int argument and returns no value , and getTemp, which accepts no value and returns an int . Assume there is a reference variable myAC to an object of this class , which has already been created. There is also an int variable called currentTemp, which has already been declared . Use the reference variable , to invoke a method to retrieve the previously set temperature and store the returned value in currentTemp.

currentTemp = myAC.getTemp();

Given that a method receives three parameters a, b, c, of type double , write some code, to be included as part of the method , that determines whether the value of "b squared" - 4ac is negative. If negative, the code prints out the message "no real solutions" and returns from the method

if((b*b - 4*a*c) < 0){ System.out.print("no real solutions"); }

Given that a method receives three parameters a, b, c, of type double , write some code, to be included as part of the method , that checks to see if the value of a is 0; if it is, the code prints the message "no solution for a=0" and returns from the method .

if(a == 0){ System.out.print("no solution for a=0"); }

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, and setting the desired temperature. The following methods provide this behavior : turnOn and turnOff, and setTemp, which accepts an int argument and returns no value . Assume there is a reference variable myAC to an object of this class , which has already been created. Use the reference variable , to invoke a method that tells the object to set the air conditioner to 72 degrees.

myAC.setTemp(72);

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off. The following methods are provide this behavior : turnOn and turnOff. Both methods take no arguments and return no value . Assume there is a reference variable myAC to an object of this class , which has already been created. Using the reference variable , invoke a method to tell the air conditioner objection to turn off.

myAC.turnOff();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off. The following methods are provide this behavior : turnOn and turnOff. Both methods take no arguments and return no value . Assume there is a reference variable myAC to an object of this class , which has already been created. Using the reference variable , invoke a method to tell the air conditioner object to turn on.

myAC.turnOn();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off. The following methods provide these behaviors : turnOn and turnOff. Both methods accept no arguments and return no value . Assume there is a reference variable officeAC of type AirConditioner. Create a new object of type AirConditioner and save the reference in officeAC. After that, turn the air conditioner on using the reference to the new object .

officeAC = new AirConditioner(); officeAC.turnOn();

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, and setting the desired temperature. The following methods provide these behaviors : turnOn and turnOff, which accept no arguments and return no value , and setTemp, which accepts an int argument and returns no value . Assume there is a reference variable officeAC of type AirConditioner. Create a new object of type AirConditioner and save the reference in officeAC. After that, use the reference to turn on the new air conditioner object and set the desired temperature to 69 degrees.

officeAC = new AirConditioner(); officeAC.turnOn(); officeAC.setTemp(69);

Write a class named Acc1 containing no constructors , methods , or instance variables . (Note, the last character of the classname is "one" not "ell".)

public class Acc1{}

Write the definition of a class Simple. The class has no constructors , methods or instance variables.

public class Simple{}

Write the definition of a method twice, which receives an integer parameter and returns an integer that is twice the value of the parameter .

public static int twice(int a){ int b=2*a; return b; }

Write the definition of a method printAttitude, which has an int parameter and returns nothing. The method prints a message to standard output depending on the value of its parameter . * If the parameter equals 1, the method prints disagree * If the parameter equals 2, the method prints no opinion * If the parameter equals 3, the method prints agree In the case of other values , the method does nothing. Each message is printed on a line by itself.

public static void printAttitude ( int x ){ if ( x == 1 ){ System.out.println ( "disagree" ) ; } if ( x == 2 ){ System.out.println ( "no opinion" ) ; } if ( x == 3 ){ System.out.println ( "agree" ) ; } }

Write the definition of a method printDottedLine, which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a newline) consisting of five periods.

public static void printDottedLine (){ System.out.println("....."); }

Write the definition of a method printGrade, which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character ) to standard output . Don't forget to put a new line character at the end of your line.

public static void printGrade(char x){ System.out.println("Grade: "+x); }

Write the definition of a method printLarger, which has two int parameters and returns nothing. The method prints the larger value of the two parameters to standard output on a single line by itself.

public static void printLarger (int x,int y){ int z = Math.max(x,y); System.out.println(z); }

Write the definition of a method dashedLine, with one parameter , an int . If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter 's value determining the number of dashes. The method returns nothing.

public void dashedLine (int a){ if (a>0){ int i; for (i=1;i<=a;i=i+1){ System.out.print("-"); } System.out.println(""); } }

Assume there is a class AirConditioner that supports the following behaviors : turning the air conditioner on and off, and checking if the air conditioner is on or off. The following methods provide this behavior : turnOn and turnOff, setTemp, and isOn, which accepts no argument and returns a boolean indicating whether the air conditioner is on or off. Assume there is a reference variable myAC to an object of this class , which has already been created. There is also a boolean variable status, which has already been declared . Use the reference variable , to invoke a method to find out whether the air conditioner is on and store the result in status.

status = myAC.isOn();


Set pelajaran terkait

AFROTC MISSION STATEMENTS/warrior knowledge

View Set

Intro to Law for Paralegals CH 5

View Set

Sports in Sales Midterm Questions and Answers

View Set

Chapter 19: Nursing Management of Pregnancy at Risk: Pregnancy-Related Complications

View Set

Quiz Muscle Conditions of the Forearm/wrist

View Set

Chapter 35: Caring for Clients with HIV/AIDS

View Set

Web Development Quiz 8 (Chapters 8 - 10)

View Set