Java MyProgramming Lab

java myprogramminglab20932

Clunker Motors Inc. is recalling all vehicles from model years 1995-1998 and 2004-2006. Given a variable modelYear write a statement that prints the message “RECALL” to standard output if the value of modelYear falls within those two ranges.

SOLUTION
if ((modelYear>=1995 && modelYear<=1998) || (modelYear>=2004 && modelYear<=2006)) {
System.out.println(“RECALL”);
}


20939
NOTE: in mathematics, division by zero is undefined. So, in Java, division by zero is always an error.

Given a int variable named callsReceived and another int variable named operatorsOnCall write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do).
HOWEVER: if any value read in is not valid input, just print the message “INVALID”.

ASSUME the availability of a variable , stdin , that references a Scannerobject associated with standard input.

SOLUTION
callsReceived = stdin.nextInt();
operatorsOnCall = stdin.nextInt();
if (operatorsOnCall == 0)
System.out.print(“INVALID”);
else
System.out.print(callsReceived/operatorsOnCall);


20943
Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear write a statement that prints the message “RECALL” to standard output if the value of modelYear falls within that range.

SOLUTION
if (modelYear >= 2001 && modelYear <= 2006)
System.out.println (“RECALL”);


20945
Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. Given a variable modelYear write a statement that prints the message “NO RECALL” to standard output if the value of modelYear DOES NOT fall within that range.

SOLUTION
if (modelYear < 2001 || modelYear > 2006)
System.out.println (“NO RECALL”);


21237
Write the definition of a method named makeStarBucks that receives a non-negative integer n and returns a String consisting of n asterisks followed by n dollars signs. So, if the method received 5 it would print:
*****$$$$$
and if received 3 it would print
***$$$

SOLUTION
static String makeStarBucks(int n, String displayStr)
{
if (n == 0)
{
return displayStr;
} // end base case
else
{
return makeStarBucks((n – 1), (“*” + displayStr + “$”));
}
}


Write a class named GasTank containing:
An instance variable named amount of type double , initialized to 0.
A method named addGas that accepts a parameter of type double . The value of the amountinstance variable is increased by the value of the parameter .
A method named useGas that accepts a parameter of type double . The value of the amountinstance variable is decreased by the value of the parameter .
A method named getGasLevel that accepts no parameters . getGasLevel returns the value of the amountinstance variable .

SOLUTION
public class GasTank
{
private double amount=0;
public void addGas(double gallon)
{
amount+= gallon;
}
public void useGas(double value)
{
amount -= value;
}
public double getGasLevel()
{
return amount;
}
}