COMP 111 Week 6 Homework Name put your name here 1 2 points

COMP 111 Week 6 Homework   Name: put your name here

1.   [2 points] Review the following code:

For each of the comment-lettered lines above, show the output and given an explanation as to why the output is as it is.

a.   Output and explanation for (a) above
Replace this text with your solution.

b.   Output and explanation for (b) above
Replace this text with your solution.

c.   Output and explanation for (c) above
Replace this text with your solution.

d.   Output and explanation for (d) above
Replace this text with your solution.


2.   [2 points] Given the following class and main method, write lines of code that would be inserted in place of the comment to solve each stated problem.

a.   Extract the second word in the string
String sub = /* your code here */;

b.   Extract the last word in the string, but not the punctuation
String sub = /* your code here */;

c.   Extract the word “Java” in the middle of the string
String sub = /* your code here */;

d.   All but the first character of the string
String sub = /* your code here */;

e.   All but the last character of the string.
String sub = /* your code here */;

3.   [3 points] Assume the following variables are declared in a main method. Evaluate each of the following expressions to get a result. Be sure to follow the order of operations.

Expression
Value of evaluated expression
a.   (tmpN - tmpX) * tmpM - tmpY / tmpN + tmpX;
Your solution goes here
b.   (tmpM / tmpN) + tmpX + tmpY;
Your solution goes here
c.   (tmpM % tmpN) + tmpX + tmpY;
Your solution goes here
d.   tmpS + tmpT;
Your solution goes here
e.   tmpS + tmpM;
Your solution goes here
f.   tmpS.substring(4, 7) + tmpT.substring(2);
Your solution goes here


4.   [3 points] Describe and correct the syntax and/or logic errors in the following snippets of code.

a.   Code below


Your solution goes here

b.   Code below


Your solution goes here

c.   Code below


Your solution goes here


5.   [5 points] Given the following code for a GasPump class (do not modify this code):

Write a driver class called GasPumpDriver that meets the following specifications:
•   Prompt the user to enter a 16 digit credit card number, read as a string
•   Prompt the user to enter a month/year from the credit card, in the format MM/YY, read as a string
•   Prompt the user to enter a dollar amount to be spent on a gas purchase, read as a double
•   Prompt the user to enter the price per gallon for unleaded gasoline, read as a double
•   Prompt the user to enter the price per gallon for premium gasoline, read as a double
•   Prompt the user to enter the price per gallon for super premium gasoline, read as a double
•   Create a GasPump object.
•   Call the set/insert methods with the dollar amounts.
•   Call the “est” methods and print the values for:
o   the number of unleaded gallons that can be purchased
o   the number of premium gallons that can be purchased
o   the number of super premium gallons that can be purchased
•   Print the credit card information, in the following format, showing only the last 4 digits of the card number:
Card: XXXXXXXXXXXX1234 Month: 10 Year: 09

Replace this text with your solution.

Solution

1) I can\'t understand what you are expecting, please clearly explain what you want.

2)

Solution:

class StringExtract
{
public static void main(String args[])
{
String sub=\"Welcome to Java DATAPRO\";
//Second word of the string
System.out.println(sub.split(\"\\\\s\")[1]);
//last word of the string
System.out.println(sub.substring(sub.lastIndexOf(\" \")+1));
//extraction the word \"Java\"
String b[] = sub.split(\" \");
for(int i = 0; i < b.length; i++)
{
if(b[i].compareTo(\"Java\")==0)
    System.out.println(b[i]);
}
//all the first characters
for(int i = 0; i < b.length; i++)
    System.out.println(b[i].charAt(0));
//all the last characters
for(int i = 0; i < b.length; i++)
{
int l=b[i].length();
    System.out.println(b[i].charAt(l-1));
}
}
}

3)Solution:

import java.util.*;
class TestChegg
{
   public static void main(String args[])
   {
       double tmpM,tmpN,tmpX,tmpY,tmp;
       String tmpS,tmpT;
       Scanner s=new Scanner(System.in);
       System.out.print(\"Enter tmpM value\");
       tmpM=s.nextDouble();
       System.out.print(\"Enter tmpN value\");
       tmpN=s.nextDouble();
       System.out.print(\"Enter tmpX value\");
       tmpX=s.nextDouble();
       System.out.print(\"Enter tmpY value\");
       tmpY=s.nextDouble();
       System.out.print(\"Enter a string\");
       tmpS=s.nextLine();
       System.out.print(\"Enter a string\");
       tmpT=s.nextLine();
         
       tmp=(tmpN - tmpX) * (tmpM - tmpY) / (tmpN + tmpX);
       System.out.println(\"the Result is \"+tmp);
       tmp= (tmpM / tmpN) + tmpX + tmpY;
       System.out.println(\"the Result is \"+tmp);
       tmp= (tmpM % tmpN) + tmpX + tmpY;
       System.out.println(\"the Result is \"+tmp);
       System.out.println(tmpS + tmpT);
       System.out.println(tmpS + tmpM);
       System.out.println(tmpS.substring(4, 7) + tmpT.substring(2));   
   }

}

5: Solution:

import java.util.*;
class GasPumpDriver
{
       String creditcardnumber;
       String mmyy;
       double amount,priceunlead,pricepremium,pricesuper;
       Scanner s=new Scanner(System.in);
       public void set()
       {
           System.out.println(\"enter the 16 digit Credit Card Number\");
           creditcardnumber=s.next();
           System.out.println(\"enter credit card expiry Month/year in mm/yy

format\");
           mmyy=s.next();
           System.out.println(\"enter a dollar amount to be spent on a gas

purchase\");
           amount=s.nextDouble();
           System.out.println(\"enter the price per gallon for unleaded

gasoline\");
           priceunlead=s.nextDouble();
           System.out.println(\"enter the price per gallon for premium

gasoline\");
           pricepremium=s.nextDouble();
           System.out.println(\"enter the price per gallon for unleaded

gasoline\");
           pricesuper=s.nextDouble();
       }
   public void est()
   {
   System.out.print(\"Credit Card Number \"+creditcardnumber);
   System.out.println(\"Mont :\"+mmyy.charAt(0)+mmyy.charAt(1)+\" Year :\"+mmyy.charAt

(3)+mmyy.charAt(4));
   System.out.println(\"Estimated Gallons of unleader for amount is

\"+(amount/priceunlead));
       System.out.println(\"Estimated Gallons of unleader for amount is

\"+(amount/pricepremium));
       System.out.println(\"Estimated Gallons of unleader for amount is

\"+(amount/pricesuper));
   }
}
class TestMain
{
   public static void main(String ar[])
   {
       GasPumpDriver gp=new GasPumpDriver();
       gp.set();
       gp.est();
      
   }
}

COMP 111 Week 6 Homework Name: put your name here 1. [2 points] Review the following code: For each of the comment-lettered lines above, show the output and giv
COMP 111 Week 6 Homework Name: put your name here 1. [2 points] Review the following code: For each of the comment-lettered lines above, show the output and giv
COMP 111 Week 6 Homework Name: put your name here 1. [2 points] Review the following code: For each of the comment-lettered lines above, show the output and giv
COMP 111 Week 6 Homework Name: put your name here 1. [2 points] Review the following code: For each of the comment-lettered lines above, show the output and giv

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site