Tina Comston Comp-111 Week 6 Franklin University |
||||||||||||||||||||||||||||||||||||||||||||||
Week 6 | ||||||||||||||||||||||||||||||||||||||||||||||
Preparation
|
||||||||||||||||||||||||||||||||||||||||||||||
Guided
Learning Activity |
||||||||||||||||||||||||||||||||||||||||||||||
Assignment
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.
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);
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
YouTube video on Java arithmetic operators https://www.youtube.com/watch?v=qpOOTYKQkDQ
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
|
||||||||||||||||||||||||||||||||||||||||||||||
Reflection
Paper
|
||||||||||||||||||||||||||||||||||||||||||||||
Lab 2
|