Java – Chapter 7

java - chapter 720638

Write the definition of a class   Telephone . The class  has no constructors , one instance variable  of type  String  called  number , and one static variable  of type  int  called  quantity .

ANSWER

public class Telephone

{

private String number;

private static int quantity;

}

20639

Write the definition of a class   Telephone . The class  has no constructors , one instance variable  of type  String  called  number , and two static variables . One is of type  int  called  quantity , initialized  to  250 ; the other is of type  double  called  total , initialized  to  15658.92 . read more

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. read more

MyProgrammingLab Java

myprogramminglab java20787 – 20931

20787

A String variable , fullName, contains a name in one of two formats:

last name , first name (comma followed by a blank), or
first name last name (single blank)
Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullNamealready initialized . You may also declare any other necessary variables .

SOLUTION
if (fullName.indexOf(“, “) != -1) {
lastName = fullName.substring(0, fullName.indexOf(“, “));
firstName = fullName.substring(fullName.indexOf(“, “) + 2, fullName.length());
} else {
firstName = fullName.substring(0, fullName.indexOf(” “));
lastName = fullName.substring(fullName.indexOf(” “) + 1, fullName.length());
} read more

Building Java Programs

Programming In JavaBuilding Java Programs: A Back to Basics Approach Plus MyProgrammingLab with Pearson eText — Access Card Package (4th Edition)

by Stuart Reges, Marty Stepp, Reges, Stuart^Stepp, Marty

Paperback, 1176 Pages, Published 2016

Layered, Back-to-Basics Approach to Java Programming

Newly revised and updated, this Fourth Edition of Building Java Programs: A Back to Basics Approach uses a layered strategy to introduce Java programming, with the aim of overcoming the difficulty associated with introductory programming textbooks. The authors’ proven and class-tested “back to basics” approach introduces programming fundamentals first, with new syntax and concepts added over multiple chapters, and object-oriented programming discussed only once readers have developed a basic understanding of Java programming. Previous editions have established the text’s reputation as an excellent choice for thoroughly introducing the basics of computer science, and new material in the Fourth Edition incorporates concepts related to Java 8, functional programming, and image manipulation. read more

Java Programming Lab

20725
Write the definition of a class Counter containing:
An instance variable counter of type int , initialized to 0.
A method called increment that adds one to the instance variable counter
It does not accept parameters or return a value .
A method called getValue that doesn’t accept any parameters
. It returns the value of the instance variable counter .

SOLUTION
public class Counter {
private int counter=0;
public void increment() {
counter++;
} public int getValue() {
return counter;}}

20726
Write the definition of a class Counter containing:
An instance variable named counter of type int .
A constructor that takes one int argument and assigns its value to counter
A method named increment that adds one to counter . It does not take parameters or return a value .
A method named decrement that subtracts one from counter . It also does not take parameters or return a value .
A method named getValue that returns the value of the instance variable counter. read more

Java Programming Lab – 20608 – 20660

20608
Write an expression that evaluates to true if and only if the integer x is greater than the integer y .
So, if the value of x was 15 and the value of y was 11, then the value of your expression would be true.
On the other hand, if the value of x was 145 and the value of y was 211, then the value of your expression would be false.
SOLUTION
x > y
————————————————————————————————————
20618
Write an if/else statement that compares the variable age with 65 , adds 1 to the variable seniorCitizens if age is greater than or equal to 65 , and adds 1 to the variable nonSeniors otherwise.
So if age has the value 73, seniorCitizens has the value 450, and nonSeniors has the value 1066,
then after your code executes seniorCitizens would then have the value 451 but nonSeniors would
still be 1066.
On the other hand, if age has the value 21, then after your code executes seniorCitizens would still
have the value 450 but nonSeniors would then be 1067.
SOLUTION
if ( age >= 65 ) seniorCitizens += 1 ; else nonSeniors += 1 ;
————————————————————————————————————
20619
Write an if/else statement that compares the value of the variables soldYesterday and soldToday , and based upon that comparison assigns salesTrend the value -1 or 1 .
-1 represents the case where soldYesterday is greater than soldToday ; 1 represents the case where soldYesterday is not greater than soldToday .
SOLUTION
if ( soldYesterday > soldToday ) salesTrend = -1 ; else salesTrend = 1 ;
————————————————————————————————————
20620
Write an if/else statement that assigns true to the variable fever if the variable temperature is greater than 98.6 ; otherwise it assigns false to fever .
SOLUTION
if ( temperature > 98.6 ) fever = true ; else fever = false ;
————————————————————————————————————
20621
Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18 , adds 1 to the variable adults if age is 18 through 64 and adds 1 to the variable seniors if age is 65 or older.
SOLUTION
if (age < 18) minors ++ ; else if (age >= 18 && age <= 64) adults ++ ; else seniors ++ ;
———————————————————————————————————— read more

Java Programming Lab 20522 – 20559

20522
Write an expression that evaluates to true if and only if the value of the integer variable x is equal to zero.
So if the value of x were 3 the expression would be false. Only if the value of x were 0 would the expression be true.
SOLUTION
x==0
————————————————————————————————————
20533
Write an expression that evaluates to true if and only if the variables profits and losses are exactly equal .
SOLUTION
profits==losses
————————————————————————————————————
20554
Given the char variable c , write an expression that is true if and only if the value of c is not the space character .
SOLUTION
c!=’ ‘
————————————————————————————————————
20555
Write an expression that evaluates to true if the value of index is greater than the value of lastIndex .
SOLUTION
index>lastIndex
————————————————————————————————————
20556
Working overtime is defined as having worked more than 40 hours during the week. Given the variable hoursWorked , write an expression that evaluates to true if the employee worked overtime.
SOLUTION
hoursWorked>40
————————————————————————————————————
20557
Write an expression that evaluates to true if the value x is greater than or equal to y .
SOLUTION
x>=y read more