Monday, June 17, 2013

MyProgrammingLab Solutions Part 2


Here is some help for MyProgrammingLab
Use a finder (Command-f on Macs) and type in your problem to find your question faster.



Rearrange the following code so that it forms a correct program that prints out the letter Q:


public class Q
{
public static void main(String[] a)
{
System.out.println("Q");
}
}


Given an integer variable  count , write a statement that displays the value of  count on the screen.

 Do not display anything else on the screen -- just the value of  count .

System.out.print (count) ;



 Given the variables  fullAdmissionPrice and  discountAmount (already declared and assigned values), write an expression corresponding to the price of a discount admission.

fullAdmissionPrice - discountAmount



 The dimensions (width and length) of room1 have been read into two variables:  width1 and  length1 . The dimensions of room2 have been read into two other variables:  width2 and  length2 . Write a single expression whose value is the total area of the two rooms.

(width1*length1)+(width2*length2)



 Assume that  word is a String variable. Write a statement to display the message "Today's Word-Of-The-Day is: " followed by the value of  word on a line by itself on standard output.

System.out.println("Today's Word-Of-The-Day is: " + word);

Write an expression that concatenates the String variable  suffix onto the end of the String variable  prefix . 

prefix+suffix

 Declare a variable  hasPassedTest , and initialize it to true. 

boolean hasPassedTest = true;


Given an  int variable  grossPay , write an expression that evaluates to true if and only if the value of  grossPay is less than  10,000 . 

grossPay < 10000

 Write an expression that evaluates to true if the integer variable  x contains an even value, and false if it contains an odd value.

x % 2 == 0


 Assume that the variables  gpa ,  deansList and  studentName , have been declared and initialized. Write a statement that adds 1 to  deansList and prints  studentName to standard out if  gpa exceeds 3.5. 

if (gpa > 3.5){
deansList += 1;
System.out.print(studentName);
}


 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.

if (age >= 65)
seniorCitizens += 1;
else
nonSeniors += 1;

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 . 


if (soldYesterday > soldToday)
salesTrend = -1;
else
salesTrend = 1;



 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. 

if (age < 18)
minors ++ ;
else if(age >= 18 && age <= 64)
adults ++;
else if (age >= 65)
seniors ++;

Given an  int variable  k that has already been declared, use a  while loop to print a single line consisting of 88 asterisks. Use no variables other than  k . 

k= 88;
while(k>0)
{
System.out.print("*");
k--;
}


Given an  int variable  n that has already been declared and initialized to a positive value, use a  while loop to print a single line consisting of  n asterisks. Use no variables other than  n . 

int k = 0 ;
while(k<=(n-1))
{
System.out.printf("*") ;
k+=1 ;
}


 Given  int variables  k and  total that have already been declared, use a  while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in  total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into  total . Use no variables other than  k and  total . 

k = 1 ;
total = 0 ;
while ( k <= 50 )
{
total = total + ( k * k ) ;
k = k + 1 ;
}


Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered.

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

do {
n = stdin.nextInt();
}
while (n>10 || n < 1);


Write a loop that reads positive integers from standard input, printing out those values that are even, each followed by a space, and that terminates when it reads an integer that is not positive. Declare any variables that are needed.

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

int x = stdin.nextInt();
while(x>0)
if(x % 2 == 0)
{
System.out.print(x+" ");
x = stdin.nextInt();
}
else {
x = stdin.nextInt();
}


 Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case,3 would be printed. 

int n, prev, consecdups=0;prev=stdin.nextInt();n=stdin.nextInt();while (n>=0) { if (n==prev) consecdups++; prev = n; n=stdin.nextInt();}System.out.println(consecdups);

Given an  int variable  n that has already been declared and initialized to a positive value, use a  do...while loop to print a single line consisting of  n asterisks. Use no variables other than  n . 

do
{
System.out.print("*");
n--; }
while (n>0);

Given  int variables  k and  total that have already been declared, use a  do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in  total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into  total . Use no variables other than  k and  total . 

for( k=1, total=0; k<=50; k++ ) total += k*k;

Given an  int variable  n that has been initialized to a positive value and, in addition,  int variables  k and  total that have already been declared, use a  do...while loop to compute the sum of the cubes of the first  n whole numbers, and store this value in  total . Use no variables other than  n ,  k , and  total . 

total = 0;
for(k = 1; k <=n; k++){
total += k * k * k;
}

Given  int variables  k and  total that have already been declared, use a  for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in  total . Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into  total . Use no variables other than  k and  total . 

k = 1;
total = 0;
while (k<=50)
{
total += (k*k);
k++;
}



Assume the  int variables  i and  result have been declared but not initialized. Write a  for loop header, i.e. something of the form 
 for (  .   .   .   ) 
 for the following loop body: 
 result = result * i; 
 When the loop terminates,  result should hold the product of the odd numbers between  10 and  20 . 

for(i=11,result=1;i<20;i+=2)

Assume the int variables i ,lo , hi , and result have been declared and that lo and hi have been initialized. Assume further that result has been initialized to the value  0 . 

 Write a for loop that adds the integers from lo up through hi (inclusive), and stores the result in result .
 Your code should not change the values of lo and hi . Also, do not declare any additional variables -- use only i ,lo , hi , and result . 


for(i = lo; i <= hi; i++)
{
result += i;
}


 Given an  int variable  n that has already been declared and initialized to a positive value, and another  int variable  j that has already been declared, use a  for loop to print a single line consisting of  n asterisks. Thus if  n contains 5, five asterisks will be printed. Use no variables other than  n and  j .     

for(j=1;j<=n;++j)
{
System.out.print("*");
}

Write a  for loop  that prints the integers 0 through 39, separated by spaces. 

for(int i=0; i<40; i++)
{
System.out.print(i + " ");
}


Write a  for loop  that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces. 


for (int i=5; i<175; i+=5) {
System.out.print(i+" ");
}



 Write a  for loop  that prints the integers 50 through 1, separated by spaces. Use no variables other than  count . 


for
( int count=50 ; count>=1; count--)
System.out.print( count + " ");



 Assume that the  int variables  i and  j have been declared, and that  n has been declared and initialized. 

 Using  for loops (you may need more than one), write code that will cause a triangle of asterisks of size  n to be output to the screen. 

for(i = 0; i < n; i++)
{
    for(j = 0; j <= i; j++)
        System.out.print("*");
    System.out.println();
}


If you can't find the answer you need on this post check my other MyProgrammingLab post for other questions and answers. If the answer still can't be found feel free to comment and let me know the question you need answered. 








3 comments:

  1. 21227 assume the availability of a method name printStars

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. how would I use a for loop or while loop to returns a random number between a low limit, and a span, for N number of iterations.

    ReplyDelete