Assignments

//**If you are unable to complete any of these assignments, you must inform me personally. Otherwise, when I check the red assignment folders I will assume you "forgot" or decided not to do the work.

Also, please label the assignment __with your name, the due date and a description__ IN CODE using the **// ** comment command .**


 * ==Due Date ................ == || == ........... Assignment== ||
 * January 11/12 || **Java PreTest**

Write a program that does the following:

Your SALE program will ask a user for the price of an item, and a % discount that should be applied. Then your program will calculate the discount on the price. Your program will show the DISCOUNT (how much they saved), as well as the FINAL PRICE. It will use one command button to display in two final labels:


 * Label One: The amount saved from original price
 * Label Two: The final price after the discount

Ensure your program has these required elements:


 * Appropriately labeled textboxes, command button, and labels
 * Code with appropriate comments
 * Meaningful variable names: you must include at least one double data type (two or more is advantageous)
 * The "parse" function for extracting values from text boxes
 * Use of the money format function to display your result with a Dollar Sign and two decimal places ($146.99)

TEST YOUR PROGRAM!!!!! An item which costs ten dollars at 15% off is a $1.50 discount for a final price of $8.50

How this program will be graded:

Design of form: Text boxes, command buttons, etc. 5 points Code including comments and variable names 10 points . ||
 * January 6 || **More Programming**

Write a program in NetBeans IDE for the following scenario:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Create a library book fine program. Create radio buttons for the following items:

Soft Cover: $ 0.25 per day Hard Cover: $ 0.50 per day VHS Tape: $ 1.00 per day DVD: $ 1.50 per day

The program should include a text box for the days that the book is overdue, then the calculate button should calculate the proper fine and display it in a label.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Use this tutorial to learn how to program radio buttons: @http://jeffancel.com/archive/2009/01/29/java-and-radio-buttons-using-netbeans-5.6-gui-builder.aspx (start at step 3 if you've already created a JFrame Form)

Some tips: Create and drop a Button Group on your form. Rename its variable name like you do with labels. Then create and format all your option buttons. Shift-click on them all, go to the "buttonGroup" properties under the "Multiple Objects - Properties" pane and select the Button Group you created. Then make ONE of your buttons "selected".

Here's the sample code from the tutorial above to do "if" statements based on option buttons selected:



. ||
 * January 5 || **On Your Own**

Write a program in NetBeans IDE for the following scenarios:


 * Rain Gauge: Five input boxes, one output label. The "Calculate Average" button averages the five values and displays them in a JLabel


 * Tax Calculator: Two input boxes, one for "Subtotal" and one for "Tax Rate as an Integer". A "Calculate Tax" button displays two labels: one has the tax cost, one has the total cost with tax. Use the money formatting technique from the December 16 lesson.


 * Grade Report: One input box for percent grade, one "Calculate Letter Grade" button that evaluates the percent and displays "A", "B", "C", "D" or "F" depending on the percentage in a JLabel.

. || //For these GUI lessons, we'll be using this tutorial, and the NetBeans IDE which will be downloaded and installed on your computers.//
 * //Note: these are MINIMUM requirements for the program. Please make sure your program has appropriate labels and design so that it is USABLE.//**
 * January 4 || **Graphics with Swing**

Graphical User Interface (GUI) Swing lessons: @http://java.sun.com/docs/books/tutorial/uiswing/index.html

1. Complete the HelloWorldSwing exercise (under "Getting Started" in the lessons above). Be sure to create the "start" directory as stated. You can run this inside JGrasp. 2. Complete all parts of the "Learning Swing with the NetBeans IDE" section of the tutorial:


 * [|Learning Swing with the NetBeans IDE]**
 * [|Setting up the CelsiusConverter Project]
 * [|NetBeans IDE Basics]
 * [|Creating the CelsiusConverter GUI]
 * [|Adjusting the CelsiusConverter GUI]
 * [|Adding the Application Logic]
 * [|Questions and Exercises: Learning Swing with the NetBeans IDE]

Print your code (20 points) as a screenshot of your running program (ensure I can see the "convertbuttonactionperformed" section of your code. Print the answers to the Questions and Exercises (10 points) . ||
 * December 21 || **Multiple Methods in Java**

Read the following websites: @http://java.sun.com/docs/books/tutorial/java/javaOO/returnvalue.html @http://math.hws.edu/javanotes/c4/s2.html

These teach us how to make multiple methods inside a class. The example of the Eck book (hws.edu) is similar to our guessing game program. For this assignment you will take your guessing game in put it in a "public static" method, then "call" that method from inside your main method. Here's an example:

code format="java5" /* Sample Multiple Method Program  */

import javax.swing.*; import java.awt.event.*;

public class SampleMultipleMethods{ public static void main(String[] args){

askName; askAge;

} // end of main method

static void askName{ String name = JOptionPane.showInputDialog(null, "What is your name?", "name", 1); JOptionPane.showMessageDialog(null, "Hi, " + name,"Hi!", 1); } // end of askName

static void askAge{ String str = JOptionPane.showInputDialog(null, "What is your age?", "Age", 1); int age = Integer.parseInt(str); if (age>=18) JOptionPane.showMessageDialog(null, "You can vote.","Of Age", 1); else JOptionPane.showMessageDialog(null, "Sorry, you can't vote ","Sorry", 1); } // end of askAge

} // end of class code Assignment:

Re-write your "Guessing Game Program" from December 15. In each of the menu options, move the code to a new "static void" method. Your menu program, then will be just a few lines, similar to this:

code format="java5" public static void main(String[] args){ String menustr; int menu = 0; do{

menustr = JOptionPane.showInputDialog(null, "Welcome to my Program\n" +                                     "   1.  Play Game\n" +                                      "   2.  About this Program\n\n" +                                      "   3.  End Program", "Welcome", 1); menu = Integer.parseInt(menustr);

if (menu==1)   playGame; else if (menu==2)    aboutMe;

}while (menu!=3); } // end of main method

code Then move the code for your guessing game into a new "static void" method called "playGame", and your about the programmer into a "static void" method called "aboutMe".

. || code Example: “The average is 50 inches. That was a moderate amount of rain” code 2. Write a program that asks the user their name, then asks for their age. Then print out a customized message based on their age, and that includes their name and age. code Example: “Dave, you are just a baby at 16 years old.” code 3. Write a program that asks a shop clerk for the subtotal amount, and then outputs the total with 6% tax. Use this website for help on formatting: @http://www.tech-recipes.com/rx/1326/java-decimal-format-to-easily-create-custom-output/ code Example: “Total: $5.30”
 * December 16 || **PRE-TEST** ||
 * || 1. Write a program that asks a user for five rainfall amounts. Then average the rainfall amounts and display the result, along with a message that says “that was a lot of rain”, “that was a moderate amount of rain”, or “that was a small amount of rain”, depending on the average.

code

//**Grading:**// 10 points each / 30 points total ||


 * December 15 || **Some more theory...**

We'll need to get a few things taken care of to be able to do math on Strings:

//**Converting String data to integer (or double) data:**//

code format="java" String str = JOptionPane.showInputDialog(null, "How old are you?", "Input", 1);

int age = Integer.parseInt(str);

code

code format="java" String str = JOptionPane.showInputDialog(null, "What's your GPA?", "Input", 1);

double GPA = Double.parseDouble(str);

code code format="java" String str = JOptionPane.showInputDialog(null, "What is your GPA?", "Input", 1);
 * //So... if you want to do math on the GPA://**

double GPA = Double.parseDouble(str);

if (GPA == 4) { JOptionPane.showMessageDialog(null, "You have an A", "Grade", 1); } else if (GPA >= 3) { JOptionPane.showMessageDialog(null, "You have a B", "Grade", 1); } else if (GPA >= 2) { JOptionPane.showMessageDialog(null, "You have a C", "Grade", 1); } else if (GPA >= 1) { JOptionPane.showMessageDialog(null, "You have a D", "Grade", 1); } else { JOptionPane.showMessageDialog(null, "You have failed.", "Grade", 1); } code


 * And so... the guessing game**

Using the skills you've learned so far in Java, it's now time to show off what you can do. Write a program that does this:

Main menu:
 * 1) Run Program
 * 2) About the Programmer
 * 3) End Program

//Run the Program:// Ask the user to guess a number between 1 and 20. The user has three tries to guess If the guess is lower than the answer, tell the user that If the guess is higher than the answer, tell the user that At the end of the program, the program will tell the user the correct answer

//About the Programmer:// Display a brief message about the program

//End Program:// um... end the program.

//**Your program must use JOptionPane for all input and output**// See THIS LINK for help

//**Grading:**// Correct use of JOptionPane ............................ 10 Main Menu functions correctly ........................ 10 Guessing game runs 3x (unless they guess) .... 10 Guessing game evaluates "too high" and "too low" .................................. 20 ......................................................... Total .... 50 Points ||
 * December 14 || **Do Now:**

Write three pseduocode programs (plain language, not Java) for three different looping structures. Recall back to our QBASIC examples: Come up with your own!
 * Bushel of apples
 * Dozen eggs


 * Looping and Increment/Decrement:**

Unary Operators: @http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html (scroll down to "Unary Operators") code format="java" int i = 1;

System.out.println("i is now worth "+ i); // prints "i is now worth 1" i++; System.out.println("i is now worth "+ i); // prints "i is now worth 2" i--; System.out.println("i is now worth "+ i); // prints "i is now worth 1" code While and Do While Loops: @http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html

While: code format="java" int count = 1;

while (count < 11) { System.out.println("Count is: " + count); count++; }

code Do...While: code format="java" int count = 1;

do { System.out.println("Count is: " + count); count++; } while (count <= 11);

code

For Loops: @http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html code format="java" for(int i=1; i<11; i++){ System.out.println("Count is: " + i); }

code

Write three programs that put your pseudocode into practice in Java. Use each of the following loop structures
 * Programming:**
 * While loop
 * Do While loop
 * For loop

//10 points for pseudocode 30 points for three loops.//
 * Grading:**

If you are finished: Create a program using dialog boxes. Use a looping structure. See THIS PAGE for help on dialog boxes. . ||
 * December 11 || %%%%%%% DETOUR %%%%%%%

Click on THIS LINK to go to today's assignment

%%%%%%% DETOUR %%%%%%% . || Pseudocode three If...Then...Else decision making algorithms of your choosing. Pick scenarios that are simple enough for you to program in VB and that use <, >, or equal to in order to make the decision.
 * December 9 || **Do Now:**

Read the section on "Equality and Relational Operators" on the webpage above. Pay close attention to "is equal to" and "is not equal to"
 * Comparison Operators:** @http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html

The syntax of If...Then in Java is much different than in Visual Basic or Basic, but once you get the hang of it, it's not tough. Basically, here is the format: code if (age >= 18) { System.out.println("You are old enough to vote"); } else { System.out.println("Sorry, grow up more"); }
 * If...Then...Else Lesson:** @http://java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html

code Not hard to understand, just hard to get the braces right. Basically each "what to do if it is true" section is in braces. You could re-write that section like this as well: code if (age >= 18) {   System.out.println("You are old enough to vote");   } else {   System.out.println("Sorry, grow up more");   }

code Here's how to add multiple else if statements: code class IfElseDemo { public static void main(String[] args) {

int testscore = 76; char grade;

if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; }       System.out.println("Grade = " + grade); } }

code Want to compare strings? Well strings aren't mathematical, so we can't use "==". Instead:

code if(str1.compareToIgnoreCase(str2) == 0){ System.out.println("String1 and String2 are equal!"); }   else{ System.out.println("String1 and String2 are not equal!"); } code And Boolean:

code format="de1" boolean OKToRide; OKToRide = true;

if(OKToRide)  // note instead of ==, you just say the boolean name {   System.out.println("It's OK to ride")    } else {   System.out.println("Do not ride")        }

code


 * Programming:**

Use the three pseudocode algorithms you wrote in the Do Now section as three separate classes (program files) in Java. Make sure at least ONE class has a basic "if...else", and ONE has several "else if" statements. Add to your algorithms if necessary to make these three classes.

//Three programs (classes) - see specifications above under "Programming". - 30 points// . || Create a "sample quiz" with five questions regarding the material discussed in the two lessons below. Make each question multiple choice, and each question should have at least four answers. Only one may be a "silly" answer, the rest should be something having to do with Java and programming. //(10 points)//
 * Grading:**
 * December 8 || **Pre-Quiz Review:** (Do Now) In preparation for the quiz, use your notes to create this:

Website from theory: @http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html Using what you've learned so far, create a simple program that uses all of the following data types:
 * After the quiz and theory:**
 * int
 * float
 * String (note the capital S)

Declare five variables using all three of the data types listed (re-use one or two). Use meaningful variable names Initialize the variables with useful data (you may do this on separate lines, or on the same line as declaration) Display the variables in a meaningful way using the System.out.println function: code System.out.println("Print your answer here" + variable + "and more" + variable);

code //10 points for sample quiz 20 points for program (10 points for use of variables, 10 points for display at runtime)// . ||
 * Grading:**
 * December 7 || Today we will talk about data types. If you miss the lecture, or miss taking notes, or want to better understand data types, please refer to this website for details: Variables and Primitive Types: @http://math.hws.edu/javanotes/c2/s2.html

Using this website: @http://java.sun.com/docs/books/tutorial/java/concepts/index.html Read ALL of the text pertaining to these two sections:
 * What Is an Object?
 * What Is a Class?

Complete the Questions (1 through 5) & Exercises (1 only) section for Tuesday at the beginning of class (Dec 8). Turn this into the Red "December 7" folder. //Questions 1-5 (1 point each) Exercise 1: Create classes (similar to the "Bicycle.java" class) for FIVE "real world" objects as discussed in the "What is the Object" section (3 points each)// . || Read each section. When asked to, compile the "Hello World" program. DO NOT use the NetBeans IDE (we will use a different one), instead follow the instructions for the "Microsoft Windows" command-line program.
 * Grading:**
 * December 3 || Using this website: @http://java.sun.com/docs/books/tutorial/getStarted/TOC.html

Then, answer the Questions and Exercises at the end of the assignment. Put your answers/code into a Word doc and turn this in. **Grading:** //7 points for questions, 3 points for both exercises// ||