Java Examples
Below is some code I did during college as examples of various tasks. This is for demonstration purposes only, but some people have found it handy.
/*Testing
*/
import javax.swing.JOptionPane;
public class skelGui
{
public static void main(String[] args)
{
String $someInput, $someOutput,$someMboxLabel;
int $someNumber;
$someMboxLabel="Label for the box";
$someNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number:"));
$someInput = JOptionPane.showInputDialog("Please enter a phrase: ");
$someOutput = "Say "+$someInput+" "+$someNumber+" times.";
JOptionPane.showMessageDialog(null, $someOutput, $someMboxLabel,JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
OR:
/*
* I knew it should have worked this way. The first exam was to enter three
* numbers and display their totals.. basically. It didn't work the way I
* wanted during the time I had, but I decided to redo it so it would. Somehow
* it just bugs me when something doesn't work the way I want.
*
* Key:
* NCCY: Not Covered In Class Yet
* UNCN: Unnecessary comment normally. As expertise with programming progresses
* the need to comment to explain what something does will decrease. You
* will probably always want to comment your code when you do something
* in an unusual way or if you are explaining why you are doing something
* but when professionally writing programs, you can assume that anyone
* reveiwing your comments will be proficient enough to understand at
* least the basics of the language. We do it differently in this class
* to establish good habits and because we are not yet proficient enough
* to be sure that our code will only be reviewed by other professionals.
*/
package numbers; //NCCY: Packages allow you to use multiple compiled
// classes for a single applicaiton.
import javax.swing.JOptionPane; //NCCY: We've covered importing, but
// this allows use of gui tools that
// this application will use.
public class NumberAdds //Class defination, we should know this.
{ // My braces are indented different than
// the book does it. I do it this way
// because I find it a lot easier to read.
// (UNCN)
public static void main(String[] args) //gotta have that main method.
{
int $arrayCt = Integer.parseInt(JOptionPane.showInputDialog("How many numbers would you like to add?"));
//Okay that should first get input with a GUI dialog
//box then turn it into an integer, then assign it into
//the newly declared integer variable $arrayCt.
//I'll use that number to size an array in a moment
//and also to determine how many times to go through
//some loops.
//(UNCN)
//
//NCCY: JOptionPane is the gui class I'm using, and
// showInputDialog is the method in that class
// I'm using. I send it the argument I want it to
// print as the prompt.
//
//Throughout this application you'll notice my
//variables all start with the $ symbol. This is my
//personal preference because I find it makes variables
//easier to notice by making them stand out. In some
//languages the $ has special meaning, in php and perl
//it is required with each variable reference and in
//shell scripting (sorta programming) it is used every
//time you want information from a variable. Its a good
//habit to be in but completely unnecessary in Java.
//I'm trying to develop the habit of keeping comments
//done with single line double slashes too. Again,
//this is a personal style preference and that is
//the only reason for it.
double[] $inputNums = new double[($arrayCt+1)];
//NCCY: this creates an array with space for one more
//variable than I will actually intend to use, because
//later a loop may hit that space and I don't want the
//program to crash if it does.
int $i;
//Short variable name but its traditional to use i as
//a loop control variable. Its probably because it is
//short to type and thats handy if you do something
//often, and you will use loops often.
//(UNCN)
double $finalSum;
//Finally something simple. This is the variable that
//will store the total of the added numbers.
//_maybe_(UNCN)
final String $STANDARD_CS_OUTPUT="Boyce \nExam 1 rewrite ver 2\n\n\n";
//We could have done this without making it a constant
//but the truth is you NEVER really need constants. You
//use them because it makes the compiles and the final
//programs function faster and with less memory.
//(UNCN)
String $userInput, $finalOutput, $mboxTitle;
//String declarations for the variable used later.
//$userInput is a string because thats how user input
//will come in, $finalOutput could be a contencation
//but this makes it easy to manipulate throughout the
//application. $mboxTitle is what I'm going to stick
//on the title bar of the final dialog. I think I can
//do it to the input boxes but I'm not at this time.
//It could be a string literal typed in when required
//of course, but this makes it easier to manipulate and
//update and also makes the code look neater later.
//(UNCN)
for($i=0;$i < $arrayCt;$i++)
//NCCY: Loops are everyone's friend. They are the
// reason we love computers. This type of loop
// is particularly handy because it does the
// initialization, comparison and incrementing
// all in one statement. Nice huh?
{
$inputNums[$i] = Float.parseFloat(JOptionPane.showInputDialog("Please enter number "+($i+1)+":"));
//NCCY: Now we're using the array. Notice how we're
// using the $i for its key. That is also why we
// initialized $i with a value of 0, see arrays
// can work all sorts of ways but the 0 based ones
// work best when you assign the first value in
// key 0, the second value would go in key 1 and
// so on. It only makes sense if you are a really
// in depth guru of programming to make a language
// work that way and many don't.. still for
// tradition's sake, we'll do it that way too.
//
// Now we're going to go through the loop as many
// times as the number of numbers the user
// indicated they wanted to enter. That is one of
// the beauties of arrays. You can handle an
// unknown number of variable values. :)
//
// One last thing to notice here, the number
// prompted for has 1 addded to it befor it is
// displayed. Thats because people aren't 0 based
// even if arrays are. ;)
}
$finalOutput = $STANDARD_CS_OUTPUT;
//The eventual display is going to start out with the
//text I use all the time.
//(UNCN)
$finalSum=0;
//Initial value should be 0.. this is kinda one of
// well duh things.. but since I didn't initialize it
// when I declared it, it needs to be done before I try
// to do anything with it, else Java's compiler throws
// a tantrum.
//(UNCN)
for($i=0;$i < $arrayCt;$i++)
//NCCY: There is that handy for loop again. This time
// we're using it to manipulate information
// instead of gathering it. The number of loops
// will be identical though.
{
$finalSum = $finalSum + $inputNums[$i];
//NCCY: Take a moment here to be awed. Here we
// are allowing the array to work its magic by handling
// some unknown number of numbers to be added together
// and the growing total to be stored in the $finalSum
// variable each time. :)
if ($i == ($arrayCt - 1))
//NCCY: Here we are making some decisions. Basically
// the program should do one thing for all the
// values except the last one, and for that last
// one it should do something different. The last
// value used will be in the key that is one less
// than the total number of values (remember, we
// are working with a 0 based array and 1 based
// human) therefore we subtract 1 from the number
// of numbers we are working with to determine
// what the key that corresponds with the last
// value will be. If it is the last one then we
// will do this bit of code. (If it isn't we'll
// handle that in a bit.)
{
if ((int)($inputNums[$i])==$inputNums[$i])
//NCCY: Yeah.. weird huh. Well first off
// what we're trying to do. We want
// numbers that get typed in as decimals
// to look like numbers without decimals // so we need to differientate between
// numbers that need to keep their
// periods and numbers that don't. This
// compares values, so if a number has
// something significant after the decimal
// then we will say that this statement
// is false and handle that in a bit, but
// if it is the same then we do this bit
// of code:
{
if ($finalSum == ((int)($finalSum)))
//NCCY: Again with the comparisons..
// thats because we also want to
// have the final sum show decimal
// places only if it needs it and
// this code only gets executed
// if we are evaluating the last
// number in the array and thus
// we also care about the final
// total's display. So here we do
// something if we are on the last
// number in the array and both
// the number we are processing
// and the final total are best
// displayed without decimals.
// (BTW, there is probably a way
// to test more than one thing
// at once but I didn't look up
// the syntax for it since this is
// actually a clearer way to write
// it sometimes.)
{
$finalOutput = $finalOutput + ((int)($inputNums[$i])) + " equals: " + ((int)($finalSum));
//In short, if we are on the
//final pass and both the users
//number and the total are
//best without decimals, then
//add them into the final
//display without decimals.
}else{
//But if the final sum is going
//to need the decimals even
//though the users last number
//didn't, then add the the
//users number without and
//the total with into the final
//display.
$finalOutput = $finalOutput + ((int)($inputNums[$i])) + " equals: " + $finalSum;
}
}else{
//On the other hand, if the users
//number did warrent decimals, then:
if ($finalSum == ((int)($finalSum)))
//we test to see if the total needs
//decimals and if it does not:
{
$finalOutput = $finalOutput + $inputNums[$i] + " equals: " + ((int)($finalSum));
//we add the user's number to
//the display with decimals but
//add the total to the final
//display without any decimals.
}else{
//and finally, if the user's
//number and total both need
//decimals then we add both
//with their decimals into
//the final display.. note
//that we're still handling
//what to do if we're on the
//last pass through the loop.
$finalOutput = $finalOutput + $inputNums[$i] + " equals: " + $finalSum;
}
}
}else{
//Now on to what to do if we're not on the
//last pass through the loop.
if ((int)($inputNums[$i])==$inputNums[$i])
//Again, we need to decide whether decimals
//are relevent or not, and if they aren't:
{
$finalOutput = $finalOutput + ((int)($inputNums[$i])) + " plus ";
//then we are going to put them in the output
//without a decimal place, thats why we
//are doing the type casting before it gets
//added to the output string.
}else{
//And if we need the decimals, then we don't
//have to typecast and we can just stick
//them in there.
$finalOutput = $finalOutput + $inputNums[$i] + " plus ";
//OH! One more thing to notice here, when we
//aren't dealing with the final pass, we just
//put users number then the word plus.. this
//is almost a hack but it is handy as heck all
//the time to do stuff this way. Remember the
//method and you'll make your life tremendously
//easier.
}
}
}
$mboxTitle = "Final Display Box";
//Since we don't need anything special in the titlebar of
//the final display, we just stick something generic in there.
//but leaving it a variable meant we could have adjusted
//its information during the program. It would optimize the
//code to redo it that way, but I like to be able to adjust
//the code easily later and if you really want optimized code
//you won't use Java anyway. Java is meant to be portable, not
//particularly fast. This could have been an issue worth
//worth addressing anyway with a constant if it had been inside
//a loop that was doing processing and needed to run as fast
//as possible.
//(UNCN)
JOptionPane.showMessageDialog(null, $finalOutput, $mboxTitle, JOptionPane.INFORMATION_MESSAGE);
//NYCC: This shows a lot more of what you can do with the
// GUI display boxes, but isn't really that complicated
// when you look at it with the variables named the way
// I did. Thats one reason to use variables instead of
// typing out what you want displayed sometimes.
System.exit(0);
//NYCC: This tells the system that it can return to regular
// processing instead of doing it inside the GUI at
// this point. And as long as we get to here, all is
// well. If we had to exit somewhere else we might exit
// with a different number so if another program were
// using this one, it could know that something unexpected
// or bad happened.
}
}
OR
/*
*Filename: Gradecaculator.java
*/
/*Algorithm:
Algorithm:
1. Get studentName
2. Get labAverage
3. Get quizAverage
4. Get exam1
5. Get exam2
6. Get exam3
7. Get finalExam
8. Calculate the examAverage with this formula:
3 Major Exams = exam1*exam2*exam3/3
9. Calculate the Final average with the following formula:
finalGrade = labAverage*0.3+examAverage*0.4+quizAverage*0.1+finalExam*0.2
10. Output the sudentName in all Caps and the finalGrade on the next line to zero decimal points.
*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Gradecaculator
{
public static void main(String[] args)
{
String studentName = new String(); //Explicit Class declaration
String inputStr = new String(); //Explicit Class declaration
double labAverage, quizAverage, exam1, exam2, exam3, finalExam, examAverage;
double finalGrade;
DecimalFormat noDigits = new DecimalFormat("0"); //Explicit Class declaration
//studentName = (String)JOptionPane.showInputDialog(null,"Enter the name of the student:","Start by entering the student's name",JOptionPane.INFORMATION_MESSAGE,null,null,null); //Step 1
//More controlled input box.
studentName = JOptionPane.showInputDialog("Enter the name of the student:"); //Step 1
//Explicit use of the class JOptionPane and method showInputDialog
labAverage = Float.parseFloat(JOptionPane.showInputDialog("Enter the lab average for "+studentName)); //Step 2
//Explicit use of the class Float and method parseFloat (and previously noted classes)
quizAverage = Float.parseFloat(JOptionPane.showInputDialog("Enter the quiz average for "+studentName)); //Step 3
exam1 = quizAverage = Float.parseFloat(JOptionPane.showInputDialog("What was the first exam grade for "+studentName)); //Step 4
exam2 = quizAverage = Float.parseFloat(JOptionPane.showInputDialog("What was the second exam grade for "+studentName)); //Step 5
exam3 = quizAverage = Float.parseFloat(JOptionPane.showInputDialog("What was the third exam grade for "+studentName)); //Step 6
finalExam = quizAverage = Float.parseFloat(JOptionPane.showInputDialog("What was the final exam grade for "+studentName)); //Step 7
examAverage = (exam1+exam2+exam3)/3; //Step 8
//JOptionPane.showMessageDialog(null,"The exam average is: "+examAverage,"Debug Pane",JOptionPane.INFORMATION_MESSAGE);
//This was a debug tool I used to find out that my math was bad in figuring the examAverage
//Previously I had been multiplying the grades instead of adding as needed.
//finalGrade = (int)(labAverage*0.3+examAverage*0.4+quizAverage*0.1+finalExam*0.2+0.5); //Step 9
//DecimalFormat isn't really necessary for this exercise since we could accomplish the same thing with this formula.
//But since the assignment is based on the book example, I've chosen to duplicate the book's method.
finalGrade = (labAverage*0.3+examAverage*0.4+quizAverage*0.1+finalExam*0.2); //Step 9
studentName = studentName.toUpperCase(); //Step 10, part 1
//Use of the class String method toUpperCase
JOptionPane.showMessageDialog(null, studentName+"\n"+"Final Grade: "
+noDigits.format(finalGrade),"Final Output",JOptionPane.INFORMATION_MESSAGE);
//Step 10, part 2
System.exit(0);
//Using class System
}
}
OR
/*
*Boyce C.
*Lab 7
*March 10, 2005
*Purpose: This application takes a series of grades then computes a final grade based
* on weighted percentages.
*Filename: Lab7.java
*/
/*Algorithm:
Algorithm:
1. Get studentName,labAvg,qzAvg,xm1,xm2,xm3,finalXm from a file.
2. Calculate the Final average with the following formula:
finGd = labAvg*0.3+(xm1*xm2*xm3/3)*0.4+qzAvg*0.1+finalXm*0.2
3. Output the studentName and the finGd (to zero decimal points) in a file.
*/
import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
public class Lab7
{
/*Here are some things that will not change throughout the execution of the program
*but which are handy to be able to change easily. Thus their inclusion as constants.
*/
static final String MY_INFO = "Boyce C.\nLab 7\nMarch 10, 2005\nLab7.java";
static final String IN_LOC = "a:\\grades.dat";
static final String OUT_LOC = "a:\\grades.rpt";
public static void main(String[] args) throws IOException, FileNotFoundException
{
/*First fufill the requirements of the class*/
JOptionPane.showMessageDialog(null,MY_INFO,"Standard Info",JOptionPane.PLAIN_MESSAGE);
String studentName, outputStr; /*There are only two strings we actually use*/
double labAvg, qzAvg, xm1, xm2, xm3, finalXm, finGd; //Step 1 begins with declarations
/*These are all the variables that we will actually need to perform calculations with
*they are shortened forms of names previously used to make the code take less time
*to type and easier to read. examAverage was removed entirely as the calculation is now
*done with a single line rather than in two parts.
*/
StringTokenizer tokCols;/*called tokCols as in tokenizer for Colons*/
DecimalFormat dig0 = new DecimalFormat("0"); /*The same as before but shorter in name*/
BufferedReader inFile = new BufferedReader(new FileReader(IN_LOC));
/*Input above and output below are how ready for use and notice the constants use*/
PrintWriter outFile = new PrintWriter(new FileWriter(OUT_LOC));
tokCols = new StringTokenizer(inFile.readLine(),"::");
/*Yup, with the declaration including the double colons it works as desired
*And then we read in the desired info below.
*/
studentName = tokCols.nextToken();
labAvg = Double.parseDouble(tokCols.nextToken());
qzAvg = Double.parseDouble(tokCols.nextToken());
xm1 = Double.parseDouble(tokCols.nextToken());
xm2 = Double.parseDouble(tokCols.nextToken());
xm3 = Double.parseDouble(tokCols.nextToken());
finalXm = Double.parseDouble(tokCols.nextToken()); //Step 1 ends after retrieving and tokenizing and parsing desired info
finGd = (labAvg*0.3+((xm1+xm2+xm3)/3)*0.4+qzAvg*0.1+finalXm*0.2); //Step 2
/*Notice the shortening of the equation to one line and because of shorter variable
*names it is tremendously more readable.
*/
outputStr = studentName.toUpperCase()+"\n"+"Final Grade: " + dig0.format(finGd);
/*output is now in a varaible to make it easier to use twice, once for display and
*once for writing to the file
*/
JOptionPane.showMessageDialog(null, outputStr,"To Write To File",JOptionPane.INFORMATION_MESSAGE);
/*First show (above,) then write (below.)*/
outFile.println(studentName+"'s grade in the couse is "+dig0.format(finGd));//Step 3
outFile.close();
System.exit(0);
/*And finally everything is closed, both the file and the gui*/
}
}
/*
*Boyce
*Lab 8b
*March 24, 2005
*Purpose: This application takes a series of grades then computes a final grade based
* on weighted percentages.
*Filename: Lab8b.java
*/
/*Algorithm:
Algorithm:
1. Get studentName, labAverage, quizAverage, exam1, exam2, exam3, finalExam
2. Calculate the Final average with the weights of 30% for lab average, 40% for
exams, 10% for quizzes and 20% for final included in the final display string
2b. Determine whether that Final average is above 59.5 for a passing grade or if it fails
3. Output the sudentName in all Caps and the finalGrade as figured at the same time
on the next line to zero decimal points. Add a note indicating whether they passed or failed.
*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Lab8b
{
static final String myInfo = "Boyce \nLab 8b\nMarch 24, 2005\nLab8a.java";
//This will be the same for each compile
public static void main(String[] args)
{
//First fufill the normal lab requirements. Using the constant becuase it is a good
//example of how and why to use them, it would slightly improve compliation time.
JOptionPane.showMessageDialog(null,myInfo,"Standard Info",JOptionPane.PLAIN_MESSAGE);
//Now we'll use the student name as a string during the program and only as a string
String studentName = new String();
//When we gather informaiton, its handy to have a place to put it sometimes.
//I don't use it in this program but it could be handy to have the code around.
//String inputStr = new String();
//We will be adding an extra note at the end of the program so this will get
//used then and it will change potentially during the execution
String noteOut = new String();
//All our inputted numbers will be doubles since we want some precision and also
//because we'll be using a decimal formatting tool that prefers a double as its
//input.
double labAverage, quizAverage, exam1, exam2, exam3, finalExam;
//The final grade should be the same type as the inputs
//Its kept separate in this program because it isn't part of the input
//but could be instantauted with the inputs.
double finalGrade;
//This is handy for making choices and can be a char since we are using
//Only letters for our grades.
char letterGrade;
//We're going to want a final output with no decimals and a correct rounding
//and the decimal formatting provides a handy way to do it.
DecimalFormat noDigits = new DecimalFormat("0");
//I kept this in as an example of a finer control.
//studentName = (String)JOptionPane.showInputDialog(null,"Enter the name of the student:","Start by entering the student's name",JOptionPane.INFORMATION_MESSAGE,null,null,null);
//Now gather inputs
//String first
studentName = JOptionPane.showInputDialog("Enter the name of the student:");
//Gathering the rest as floats even though they are actually doubles because
//the float will be converted to a double when assigned but float is sufficient
//precision for the actual input.
labAverage = Float.parseFloat(JOptionPane.showInputDialog("Enter the lab average for "+studentName));
quizAverage = Float.parseFloat(JOptionPane.showInputDialog("Enter the quiz average for "+studentName));
exam1 = Float.parseFloat(JOptionPane.showInputDialog("What was the first exam grade for "+studentName));
exam2 = Float.parseFloat(JOptionPane.showInputDialog("What was the second exam grade for "+studentName));
exam3 = Float.parseFloat(JOptionPane.showInputDialog("What was the third exam grade for "+studentName));
finalExam = Float.parseFloat(JOptionPane.showInputDialog("What was the final exam grade for "+studentName));
//Calculating the final grade here so it can be used for evaluations
finalGrade = (labAverage*0.3+((exam1+exam2+exam3)/3)*0.4)+quizAverage*0.1+finalExam*0.2;
//Now do the evaluations, note the .5's because they make it more obvious that
//the rounded number is actually used in the output.
if (finalGrade >= 89.5)
letterGrade = 'A';
else if (finalGrade >= 79.5)
letterGrade = 'B';
else if (finalGrade >= 69.5)
letterGrade = 'C';
else if (finalGrade >= 59.5)
letterGrade = 'D';
else
letterGrade = 'F';
//end nested if statement
//Now we don't really need this part but it makes a nice example for switches
switch(letterGrade)
{
case 'A' : noteOut = "You rock! (You have an A)";
break;
case 'B' : noteOut = "You did well. (You have an B)";
break;
case 'C' : noteOut = "How does it feel to be mediocre? (You have an C)";
break;
case 'D' : noteOut = "You managed to pass! (You have an D)";
break;
default : noteOut = " Looser! (You have an F)";
}//End switch
//Finally spit out the results of running the program.
JOptionPane.showMessageDialog(null, studentName.toUpperCase()+"\n"+"Final Grade: "
+noDigits.format(finalGrade)+" ("+letterGrade+")\n" + noteOut,"Final Output",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
/*
*Boyce
*Lab 9
*April 7, 2005
*Purpose: This application takes a series of grades then computes a final grade based
* on weighted percentages.
*Filename: Lab9.java
*/
/*Algorithm:
Algorithm:
1. Get studentName, labAverage, quizAverage, exam1, exam2, exam3, finalExam
2. Calculate the Final average with the weights of 30% for lab average, 40% for
exams, 10% for quizzes and 20% for final included in the final display string
2b. Determine the final grade letter associations
3. Output the sudentName in all Caps and the finalGrade as figured at the same time
on the next line to zero decimal points. Add a note indicating their grade.
*/
import javax.swing.JOptionPane;
import java.text.*;
import java.util.*;
import java.io.*;
public class Lab9
{
static final String myInfo = "Boyce \nLab 9\nMarch 24, 2005\nLab9.java";
//This will be the same for each compile
public static void main(String[] args) throws IOException, FileNotFoundException
{
//First fufill the normal lab requirements. Using the constant becuase it is a good
//example of how and why to use them, it would slightly improve compliation time.
JOptionPane.showMessageDialog(null,myInfo,"Standard Info",JOptionPane.PLAIN_MESSAGE);
//Now we'll use the student name as a string during the program and only as a string
String studentName = new String();
//When we gather informaiton, its handy to have a place to put it sometimes.
//I don't use it in this program but it could be handy to have the code around.
//String inputStr = new String();
//We will be adding an extra note at the end of the program so this will get
//used then and it will change potentially during the execution
String noteOut = new String();
//All our inputted numbers will be doubles since we want some precision and also
//because we'll be using a decimal formatting tool that prefers a double as its
//input.
double labAverage, quizAverage, exam1, exam2, exam3, finalExam;
//The final grade should be the same type as the inputs
//Its kept separate in this program because it isn't part of the input
//but could be instantauted with the inputs.
double finalGrade;
//This is handy for making choices and can be a char since we are using
//Only letters for our grades.
char letterGrade;
//Declaring the tokenizer
StringTokenizer tokenizer;
//We're going to want a final output with no decimals and a correct rounding
//and the decimal formatting provides a handy way to do it.
DecimalFormat noDigits = new DecimalFormat("0");
//I kept this in as an example of a finer control.
//studentName = (String)JOptionPane.showInputDialog(null,"Enter the name of the student:","Start by entering the student's name",JOptionPane.INFORMATION_MESSAGE,null,null,null);
String inputLine;
BufferedReader inFile = new BufferedReader(new FileReader("a:\\student.dat"));
inputLine = inFile.readLine();
//Now gather inputs
while(inputLine != null)
{
tokenizer = new StringTokenizer(inputLine,"::");
studentName = tokenizer.nextToken();
labAverage = Double.parseDouble(tokenizer.nextToken());
quizAverage = Double.parseDouble(tokenizer.nextToken());
exam1 = Double.parseDouble(tokenizer.nextToken());
exam2 = Double.parseDouble(tokenizer.nextToken());
exam3 = Double.parseDouble(tokenizer.nextToken());
finalExam = Double.parseDouble(tokenizer.nextToken());
//Calculating the final grade here so it can be used for evaluations
finalGrade = (labAverage*0.3+((exam1+exam2+exam3)/3)*0.4)+quizAverage*0.1+finalExam*0.2;
//Now do the evaluations, note the .5's because they make it more obvious that
//the rounded number is actually used in the output.
if (finalGrade >= 89.5)
letterGrade = 'A';
else if (finalGrade >= 79.5)
letterGrade = 'B';
else if (finalGrade >= 69.5)
letterGrade = 'C';
else if (finalGrade >= 59.5)
letterGrade = 'D';
else
letterGrade = 'F';
//end nested if statement
//Now we don't really need this part but it makes a nice example for switches
switch(letterGrade)
{
case 'A' : noteOut = "You rock! (You have an A)";
break;
case 'B' : noteOut = "You did well. (You have an B)";
break;
case 'C' : noteOut = "How does it feel to be mediocre? (You have an C)";
break;
case 'D' : noteOut = "You managed to pass! (You have an D)";
break;
default : noteOut = " Looser! (You have an F)";
}//End switch
//Finally spit out the results of running the program.
JOptionPane.showMessageDialog(null, studentName.toUpperCase()+"\n"+"Final Grade: "
+noDigits.format(finalGrade)+" ("+letterGrade+")\n" + noteOut,"Final Output",JOptionPane.INFORMATION_MESSAGE);
inputLine = inFile.readLine();
}//End While loop
System.exit(0);
}
}
/*
*Boyce
*Lab 10
*April 12, 2005
*Purpose: This application prompts the user for a number then displays a line
* with the same number of asterisks, and so long the number of asterisks
* is greater than zero, prints another with one less until there are zero
* asterisks
*Filename: Lab10.java
*/
/*Algorithm:
Algorithm:
1. Get integer userNumber
2. Print a line with userNumber of asterisks
3. Subtract one from the inputed number
4. Repeat line 2 and 3 until userNumber is less than one
*/
import java.io.*;
public class Lab10
{
static BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException
{
System.out.print("Enter an integer: "); //Step 1
//Step 3 and 4
for(int userNum = Integer.parseInt(keyin.readLine());userNum > 0;userNum --)
{
for(int count=userNum;count>0;count--)
System.out.print("*");//Step 2a
System.out.print("\n");//Step 2b
}
}
}