Tina Comston
Comp-111 Week 6
Franklin University
Week 6
Preparation
    • Read Chapter 4 (sections 4.3 - 4.5) in the COMP 111 Reading Guide.
      Guided Learning Activity
      Assignment
      • Problem 1
      Concatenation

      Concatenation is the merging together of different data types.

      When merging together numeric data types, the result will be numeric.  When merging a numeric data type with a string data type, the result will be string, appending the number onto the string.
      • Problem 2
        • To extract a portion of a string, the substring() method is used.  When using this command, keep in mind how the characters of a string are counted.  When assigning numbers to the characters in a string, you must start with 0, not 1.  Consider the following string:
      HELLO
      01234
        • To refer to the first character you would be referring to position 0.  To refer to the last character, you would use 4.
        • If you wanted to extract   ELLO, omitting the H, you would be extracting positions 1 through 4.
      Substring
      The substring method accepts 1 implicit parameter and 1 or more explicit parameters.  The format is:

          strValue.substring(start, end);

      strValue is the implicit parameter.  It is the string object that is calling the substring method.

      start is the starting position in the string, this says to start at this  character  (inclusive)

      end is the ending position in the string - BUT this says to go up to but do not include this position  (exclusive)


          String strValue = "This is a string";

       strValue.substring(3, 7);

      Thisisastring
      0123456789012345


      start with position 3 inclusive
      end with position 7 exclusive

      The value that would be returned is:  "s is".

      Now - remember this returns the value, it does not modify the original string.  The example given above would return "s is", but unless this is put into another string the returned value would cease to exist as soon as the method call is over.  To retain the value we must assign it to another variable.

      String newString = strValue.substring(3, 7);

      end position - this explicit parameter is optional.  If it is not included the substring will method take all of the remaining characters.

      String newString = strValue.substring(3);

      This will start at position 3 and return all of the characters to the end.

      "s is a string"


      YouTube video on concatenation & substrings https://www.youtube.com/watch?v=USmdNcjyJNU
      • Problem 3
        • The order of precedence is not something new.  This had been around since your high school algebra days.  It is:
          • P - parenthesis
          • E - exponent
          • M - multiplication
          • D - division
          • Mo - Modulus
          • A - addition
          • S - subtraction
        • In addition to the order of precedence, you must also keep in mind a couple of rules specific to Java itself.
          • The + operator concatenates two strings, provided one of the expressions, either to the left or right of the + operator is a string. 
            • If you have "test" + 4, you will end up with the string "test4".
            • If you have 4 + "test", you will end up with the string "4test".
            • If you have 4 + 7, you will end up with an integer data type 11 - neither of the operators was a string, so concatenation does not occur.
          • When dividing, you must consider the data types of the values being divided.  
            • If both the numerator and denominator are type integer, then the result will also be integer - no decimal places given, they are truncated.
            • If either the numerator or denominator are type double, then the result will also be double - giving decimal places in the result.  
          • When a double value is cast into an integer variable, the decimal portion is discarded.  No rounding occurs.  
      YouTube video on Java arithmetic operators  https://www.youtube.com/watch?v=qpOOTYKQkDQ
      • Problem 4
        • Determine the logic errors in the given snippets of code.
      • Problem 5
      Scanner class

      The Scanner class is utility class provided by Java that allows a programmer to accept input from the console.  Before the Scanner class can be used, you just import the class from the Java utility library.  This is done by an import statement that is placed above the class itself - up at the very top.

      import java.util.Scanner;

      Then inside of your code you can create a Scanner object.

      Scanner input = new Scanner(System.in);

      The Scanner object is provided one explicit parameter "System.in" which tells Java that you want to accept input from the console.

      The variable name "input" that I provided above is just a variable name.  This could be anything, but should be meaningful.  I chose "input" to indicate the type of data, but you could use "in", "inData", etc.

      The Scanner class provides 4 methods that allow you to accept data from the console.

      .next() - returns up to the next space
      .nextLine() - returns up to the next line
      .nextInt() - returns an integer value
      .nextDouble() - returns a double value

      Here's an example:

      Scanner input = new Scanner(System.in);
      System.out.println("Enter your full name");
      String enteredValue = input.next()
            user enters "Joe Smith"

      Because the .next() method returns just to the next space, enteredValue will contain "Joe"

      If you replace input.next() with input.nextLine(), enteredValue would contain "Joe Smith"

      ***Also to note - that above I first told the user what I wanted them to enter with the System.out.println() statement.  Don't make your user guess that they should enter data, tell them.

      YouTube video on the Scanner class https://www.youtube.com/watch?v=boiILvueLiA

      You have been provided with the code for the GasPump class.  In BlueJ create a class with the name GasPump.  Delete the code inserted into the class by BlueJ.  Copy the code provided in the homework and paste it into the BlueJ class.  Compile.

      Create a new class with a name GasPumpDriver.  In this class you will be creating a GasPump object.  You will then use the values entered by the user to call the various methods on your GasPump object to determine the number of unleaded, premium, and super premium gallons that can be purchased.


       
      Exam 1
      • It's a good idea to contact your proctor on Monday to ensure they have received the test and to set up a time for taking the test.  Please allow 2-3 hours for taking the test.  Remember that it must be taken all at one time - no breaks are permitted.  
      • No books and No notes are permitted to be used when taking the test.  All that is permitted are writing utensils and scrap paper.  

      Reflection Paper
      • Don't forget to record your reflections for this week.
      Lab 2
      • Although Lab 2 is not due this week, it's best not to wait to begin working on this project.  Read through the instructions. Make sure you have a clear understanding of what is needed.