Tutorial 2 Lecturer: Ken Jackson Tutor: Andria Hunter ========== Friday Tutorial Section: 980116 @1pm (MP102) Tuesday Tutorial Section: 980120 @6pm (RW142) CSC 108, Spring 1998 Tutorial notes, T2 ================================================================== Tutorial Topics: - Take up Assignment 0 - Bug in Visual Age's input - "Doing your work in CSC108" handout - "Print Java Assignment" command - Questions about Assignment 1 You should bring the handout "Doing your work in CSC108" with you to the tutorial. If you did not figure out the solution to Assignment 0 on your own, I recommend that after this tutorial you go to the CDF-PC lab and type in and run Assignment 0. You should practice using the "Print Java Assignment" command by printing Assignment 0. Then begin with Assignment 1 because it is due in the next tutorial (tutorial 3). The main CSC108 web page has more information about how to print your assignment using the "Print Java Assignment" command. If you get stuck, be sure to take a look at that page. ----------------------------------- Assignment 0 Solution ===================== package asst0; import java.io.*; // CSC108 Assignment #0 // Name: Iam Me Student ID: 555555555 // Tutor: Andria Hunter Prof: Ken Jackson // // Program Description: Reads a term mark and a final // exam mark and calculates and reports a course grade. class Grade { // Main method to read marks and display final grade. public static void main (String[] args) throws IOException { // Declare stdin so data can be read from input. DataInputStream stdin = new DataInputStream (System.in); final double TERM_WEIGHT = .55; // Term mark percentage final double EXAM_WEIGHT = .45; // Exam mark percentage int term_mark, exam_mark; // Term and exam marks double result; // Final mark // Read in the term mark and exam marks from stdin System.out.println ("Enter the term mark"); term_mark = Integer.parseInt(stdin.readLine()); System.out.println ("Enter the exam mark"); exam_mark = Integer.parseInt(stdin.readLine()); // Calculate the final course mark and display it result = TERM_WEIGHT * term_mark + EXAM_WEIGHT * exam_mark; System.out.print ("Here's the final grade "); System.out.println (result); } } Sample Input 80 60 Sample Output Enter the term mark Enter the exam mark Here's the fial grade 71.0 NOTE: Instead of having the constants declared as "final" within the main method, you may have instead declared them as "static" above the main method (within the Grade class). ----------------------------------- Bug in Visual Age's Input ========================= There's a bug in the keyboard input from Visual Age's Console window. The effect is that the Enter key seems to have no effect when you use the style of input described in the text. To avoid the problem, then, do not do this to set up your program's input stream: BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); String inputLine = stdin.readLine(); Instead, use an old-fashioned version of Java's input, like this: DataInputStream stdin = new DataInputStream (System.in); String inputLine = stdin.readLine(); However, you can use BufferedReaders if you export the compiled class. That is, make a file with a name ending ".class" by selecting Visual Age's Export command and clicking in the right places to get .class files. The exported file will work fine with the "java" command that you can download from the Sun web site. (See the CSC108 home page for the location.) You probably don't want to do that, but it does show that the problem is not likely to be very hard to fix. ----------------------------------- Doing your work in CSC108 ========================= How to submit your assignments ------------------------------ - staple pages together (don't put everything in an envelope) - include java program file, input, and output (Use PJA) - do not include disks unless asked - label your program with your name, student ID, CSC108, Assignment #, Tutor name, and Prof name. [You can make a title page if you want. This should be included in the comments near the top of your program.] The work you submit must be your own ------------------------------------ - no copying - never discuss code How to do well on your assignments ---------------------------------- - you'll be marked for both correctness and style - even if your program works perfectly, you may only get 5/10 if you have very poor style! Some excerpts from the tutors' marking guide -------------------------------------------- Completeness * The program should not only respond correctly in the standard cases but also deal with the required special cases. * Is 0 an acceptable input? a negative value? a list of 0 items? Sometimes a sensible response can be given to unusual input; for example, the sum of the first 0 terms of a series is 0, but the sum of the first -3 terms may reasonably be considered nonsensical. The program should handle the "special but sensible" cases properly, and produce suitable error messages in other cases. Style * Are the variable names chosen well? Are the variables declared in the right place (where they're needed, not at the beginning of the program)? * Are "magic numbers" avoided and constants declared instead? * Are the blocks of the program-loops, if statements, methods, and classes-well chosen and well laid out? For example, are two loops used where one would do? * Is the program indented properly ("paragraphed"-indented to show its structure.)? * Are blank lines used to advantage (also called "whitespace")? Don't leave too much space, but make sure you leave blank lines around each major sectio of code. * Is the user given appropriate messages? For example, when input is needed from the user, there should be a prompt; and output messages should be formatted to be readable. If you have trouble deciphering the output, there is something wrong. Comments * Are there clear comments explaining each class and method? * Are there clear comments explaining each major block of code? * Are important variables defined in comments? Clearly? * Is there an initial comment stating the purpose of the program and giving the student's name, student number, course and assignment number, tutor's name, and prof's name? Testing * Are the standard cases covered? the obvious boundary cases? the not- so-obvious special cases? the error cases their programs are supposed to cover? * Are the test outputs annotated so you can tell what they're supposed to prove? For example, if you are showing that your test works when the user doesn't enter any course marks, you would write that on your printout afterwards. ----------------------------------- Print Java Assignment ===================== - You can access this program from the "applications" folder on your desktop. - Make sure the class that contains the main method has been declared as public. public class MyClass { ... - Make sure you have exported both class and java files to the H:\printout directory - Make sure you have used the scrapbook editor to create your test files, and you have saved them on the H: drive somewhere. Now you're ready to run Print Java Assignment --------------------------------------------- 0. Click on the "Print Java Assignment" icon. You'll see a black MS-DOS window; ignore it. Now Print Java Assignment asks several questions. When it has your answers, it gives you your output. 1. Enter the name of the program to submit. Type the name of the class containing the "main" method that you want to run. You have to include the name of the package the class belongs to (unless you are using the default package). For example, if your class is "MyClass" and it's in the package "pkg", give this program name: pkg.MyClass But if you are using the default package, just use the class: MyClass This window also gives you three options: 1. "Echo Input" - select 2. "Pretty Print Source" - select 3. "Preview Result" - do not select 2. "Select Source Files". That means files with names ending .java. The available files are listed on the left, and you can click on them to transfer them to the right-hand column. If you're in the wrong directory, double-click directory names to change; double-clicking ".." moves you up to the next level of directory. 3. "Select Input Files". This is like the previous question, only now you're picking the data files where your input will come from. You can pick as many as you like, and your program will be run separately on each data file. That is, for each data file, the program is run once, taking its input data just from that file. 4. Now, if your output is being sent to a printer rather than the screen, you get the usual "choose a printer" dialogue. When you're finished with that, your program and its output are printed.