eCheck04B can be found on page 172 of the textbook.
You should try to compile and run all of the examples in this tutorial.
You should follow all of the links in this tutorial and study the APIs that the links take you to.
All of the classes you use today are fair game for subsequent tests.
Stock
ClassThe Stock
class encapsulates the idea
of an investment that represents a share of ownership
in a corporation.
A Stock
object has several attributes
such as a:
Other attributes can be found in the API.
Stock
ObjectThe Stock
class has two constructors that allow
a client to create an instance of a Stock
.
The default constructor creates a Stock
object with no name, no symbol, and a price of 0.0
import type.lib.Stock; import java.io.PrintStream; public class Stock1 { public static void main(String[] args) { PrintStream output = System.out; Stock s = new Stock(); output.println("Name : " + s.getName()); output.println("Symbol : " + s.getSymbol()); output.println("Price : " + s.getPrice()); } }
Notice the use of new
to create
the Stock
object.
Notice that the client can use the accessor methods
getName
,
getSymbol
,
and getPrice
to get the values of the name,
symbol, and price, respectively.
Stock
ObjectThe one-argument constructor creates a Stock
object given its symbol. Look at the API for a description
of valid symbols.
import type.lib.Stock; import java.io.PrintStream; public class Stock2 { public static void main(String[] args) { PrintStream output = System.out; Stock s1 = new Stock("HR.A"); output.println("Name : " + s1.getName()); output.println("Symbol : " + s1.getSymbol()); output.println("Price : " + s1.getPrice()); output.println(); Stock s2 = new Stock(".MN"); output.println("Name : " + s2.getName()); output.println("Symbol : " + s2.getSymbol()); output.println("Price : " + s2.getPrice()); } }
Scanner
to Read a String
A Scanner
object has two methods to scan for an
arbitrary String
.
One method is named next
. It behaves like
nextInt
and nextDouble
in that it
skips over whitespace trying to find a String
(a sequence of characters without whitespace) to return.
import type.lib.Stock; import java.io.PrintStream; import java.util.Scanner; public class Stock3 { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.print("Enter a stock symbol : "); String symbol = input.next(); Stock s = new Stock(symbol); output.println("Name : " + s.getName()); output.println("Symbol : " + s.getSymbol()); output.println("Price : " + s.getPrice()); output.println(); } }
Note that next
(and most of the other
next
-like methods in Scanner
)
does not consume the newline character at the end of
the input. This means that the newline character the user
inputs by pressing the Enter key remains to be
processed by the Scanner
. This is
important to remember when using nextLine
!
Scanner
to Read a String
A second method is named nextLine
. Unlike
the other next
-like methods, this method does
not ignore newlines. It skips past the current line (i.e.
it skips past the first newline character that if finds)
and returns what it skipped over. If there is a
newline character at the beginning of the input,
nextLine
skips over it and returns what was
skipped (i.e. it returns an empty String
).
import type.lib.Stock; import java.io.PrintStream; import java.util.Scanner; public class Stock4 { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.print("Enter an integer : "); int anInt = input.nextInt(); output.print("Enter a stock symbol : "); String symbol = input.nextLine(); Stock s = new Stock(symbol); output.println("Name : " + s.getName()); output.println("Symbol : " + s.getSymbol()); output.println("Price : " + s.getPrice()); output.println(); } }
Notice that nextInt
leaves the newline character
on the input to the Scanner
object.
Can you fix the example above so that it
actually allows the user to input a symbol?
A java.util.Date
(API) object can be used to get
the time at which it was created. If you create a
Date
object and then immediately ask it for the time
it will return the approximate current time.
import java.io.PrintStream; import java.util.Date; public class DateExample { public static void main(String[] args) { PrintStream output = System.out; Date d = new Date(); String dString = d.toString(); output.println(dString); } }
You should now have enough information to complete eCheck04B.