Casual Discussion, Warm-up Questions, and Lecture Demo Exercises

advertisement
Lecture Exercise 03
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Casual Discussion, Warm-up Questions, and Lecture Demo Exercises
1. [Lab02-Q3] Scanner object for file reading: Scanner inFile = new Scanner(new File(filepathname));
Your task: Rewrite the statement as guided below:
File f;
f = ____________________________;
//Create the File object
(Note: the object variable refers to the object)
Scanner inFile;
//Declare a variable for a Scanner object
inFile = _______________________;
//Create the Scanner object to access the file
2. Complete the program below according to the sample rundown:
public static void main(String[] args) { String name; int age; Sample rundown:
What is your name? Helena WONG How old are you? 28 Hello, Helena WONG. Next year, you'll be 29 Underlined content is input by the user
System.out.print("What is your name? "); System.out.print("How old are you? "); System.out.println("Hello, " ". Next year, you'll be " ); s.close(); }
3. [Topic02 - Page 10]
Scanner in = new Scanner(System.in);
String s1,s2; s1 = in.next(); //type " Today is a good day." s2 = in.nextLine(); System.out.println(s1); //"Today" System.out.println(s2); //" is a good day“ in.close(); Rundown:
Today
is a good day.
Today
is a good day.
Fill in the following to explain what happens.
i.
When s1 = in.next() runs, the program pauses for user input.
ii.
After we type " Today
is a good day.<enter>", the program wakes
up and ___________to finish s1 = in.next().
iii. s1 = in.next() skips any __________ and then only reads one word
(ie. up to _______________). Therefore s1 gets "Today".
Choose words from below:
pauses
continues
leading spaces
white-space (space,tab,'\n')
input stream
remaining contents
discarded (dropped)
<space><space><space><space>
iv. "<space><space><space><space>is a good day.<enter>" is left over in
the_______________________.
v.
Then s2 = in.nextLine() runs, it doesn't pause for user input but
simply gets the _________________in the input stream (until <enter>;
and <enter> is _____________.). Therefore s2 gets
"______________is a good day."
Last modified: 29‐Jan‐2016 1
Lecture Exercise 03
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
4. [Topic03 - Page 3] Explain the output statement in detail steps.
5. Refer to Lab02 Q4,
Please input the
0
0
0 36
0
0
0
0
0
0 14
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0 31
0
0
0
0
0
39
0 37
0
Maximum row sum:
Maximum col sum:
file pathname: c:\2.txt
0
0
0
0
0
0
0
0
0
3
0 29
0
0
0
0
0
4
0
0
0
0
0
0
0
0 21
0
0
0
0
0
9
0
0
0
0 30
0
0
0 18
45
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
76 (row 7 9)
82 (col 2)
An approach is:
- Implement 2 small helper methods:
private int getRowSum(int r) //Return the sum of row r
private int getColSum(int c) //Return the sum of column c
-
Then apply the above in
public
public
public
public
int getRowSumMax() //return the maximum sum among the rows
int getColSumMax() //return the maximum sum among the columns
void printAllRowsOfMax() //print the row numbers which contain the max
void printAllColsOfMax() //print the col numbers which contain the max
Your task: Complete the code on next page.
Last modified: 29‐Jan‐2016 2
Lecture Exercise 03
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
public static void main(String[] args) throws FileNotFoundException
{
System.out.print("Please input the file pathname: ");
Scanner scannerObj = new Scanner(System.in);
String fileName = scannerObj.nextLine();
Table2dMxSumRowCol table = new Table2dMxSumRowCol(fileName);
table.print();
System.out.print("Maximum row sum: "_________________________);
System.out.print(" (row ");
_____________________________//Call table.printAllRowsOfMax()
System.out.println(")");
System.out.print("Maximum col sum: "_________________________);
System.out.print(" (col ");
_____________________________//Call table.printAllColsOfMax();
System.out.println(")");
scannerObj.close();
}
public class Table2dMxSumRowCol {
private int[][] nums;
//.. some methods
private int getRowSum(int r)
{
}
public int getRowSumMax()
{
}
public void printAllRowsOfMax()
{
}
public int getColSumMax(){..} //Similar to getRowSumMax
private int getColSum(int c){ .. } //Similar to getRowSum
public void printAllColsOfMax(){..} //Similar to printAllRowsOfMax
}
Last modified: 29‐Jan‐2016 3
Lecture Exercise 03
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
6. [Topic03- Page 5] Add the following constructors
7. [Topic03- Page 6], what if we do another testing as shown below:
Last modified: 29‐Jan‐2016 4
Download