Sponsor Advertisements help keep UGN Security Online.
Sponsor Advertisements help keep UGN Security Online.
Want to earn prizes for clicking online advertisements? Join Rewards1.com.
|
|
|
#19175 - 09/22/04 09:39 AM
Second Java Program (what u think ?)
|
UGN News Staff
Registered: 11/29/02
Posts: 1146
Loc: Canada
|
Well, I just started learing Java couple days ago, this is the second program i made.. the first one is really crapy, just fooling around with some basic stuff. So i decided to use a IDE and started making this program. Its for school and it you made it in the "Consol" but i started fooling around with it and made it a little better. If ur wondering why the seond program goes to the console, gets the Grade, then goes back to Messages, its because Java cant use the variable String in switch statements, and i read some tutorials on connverting the string to a hash (Just got confused). So i just used this way lol. If the Comments dont seem correct, dont Flame  i am new to this language. Heres my Code: import javax.swing.JOptionPane; // Import the Class for the JOptionPane
import java.lang.String; // Import the Class for the String variable
/*
Name: Ice
Date: September 21, 2004
Exercise: Assignment #2
*/
public class SecondAssignment {
public static void main(String[] Args) {
int theChoice; // Declares all the Variables Needed
int userAge;
char theGrade;
//Prompt the user to Pick the Assignment they wanna see and store it in userChoice
String userChoice = JOptionPane.showInputDialog(null, "Assignment #2" + "\nPlease Pick the Assignment you would like to see: "
+ "\n(1) - The Age Program" + "\n(2) - The Switch Program", "Assigment #2", JOptionPane.INFORMATION_MESSAGE);
//Convert the String to a Integer and store it in theChoice so a Switch statment can be used
theChoice = Integer.parseInt(userChoice);
switch (theChoice) {
case 1: // First Case
//Asks the user How old they are and stores it in firstChoice
String firstChoice = JOptionPane.showInputDialog(null, "Please Enter How Old you Are:", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
userAge = Integer.parseInt(firstChoice);
// IF/ELSEIF statment to check that message to print out to the user
if (userAge <= 1)
JOptionPane.showMessageDialog(null, "Infant", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge == 2)
JOptionPane.showMessageDialog(null, "Toddlert", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 3 && userAge <= 5)
JOptionPane.showMessageDialog(null, "Preschool", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 6 && userAge <= 10)
JOptionPane.showMessageDialog(null, "Child", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 11 && userAge <= 13)
JOptionPane.showMessageDialog(null, "Preteen", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 14 && userAge <= 18)
JOptionPane.showMessageDialog(null, "Teenager", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 19 && userAge <= 26)
JOptionPane.showMessageDialog(null, "Young Adult", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 27 && userAge <= 45)
JOptionPane.showMessageDialog(null, "Adult", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 40 && userAge <= 60)
JOptionPane.showMessageDialog(null, "Older Adult", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else if (userAge >= 66 && userAge <= 100)
JOptionPane.showMessageDialog(null, "Senior", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Wise Old Sage", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
break;
case 2: // Second Case
// Ask the user for there Grade and store it inside theGrade char Variable
System.out.print("Please Enter your Grade: ");
theGrade = in.getChar();
//Does the Switch Statement to see witch Message to print from the user
switch (theGrade) {
case 'A': JOptionPane.showMessageDialog(null, "Excellent", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
break;
case 'B': JOptionPane.showMessageDialog(null, "Good", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
break;
case 'C': JOptionPane.showMessageDialog(null, "Average", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
break;
case 'D': JOptionPane.showMessageDialog(null, "Poor", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
break;
case 'F': JOptionPane.showMessageDialog(null, "Unsatifactory", "Assignment#2", JOptionPane.INFORMATION_MESSAGE);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Grade Assignment", "Assignment#2", JOptionPane.WARNING_MESSAGE);
break; }
System.exit(0); // End the Program with nothing being stored into memory (Realese all Variables)
}
}
} Tell me what u guys think, and what i can do to improve :]
_________________________
Good artists copy, great artists steal.
-Picasso
|
|
Top
|
|
|
|
Sponsor Advertisements help keep UGN Security Online.
Sponsor Advertisements help keep UGN Security Online.
|
|
#19177 - 10/12/04 10:54 PM
Re: Second Java Program (what u think ?)
|
UGN News Staff
Registered: 11/29/02
Posts: 1146
Loc: Canada
|
i think Java is a weak Language thou  Its pretty sweet, but very weak at times.
_________________________
Good artists copy, great artists steal.
-Picasso
|
|
Top
|
|
|
|
#19179 - 10/13/04 04:58 AM
Re: Second Java Program (what u think ?)
|
UGN News Staff
Registered: 11/29/02
Posts: 1146
Loc: Canada
|
I think C++ is pretty good. Some good jobs out there if your good with C++.
_________________________
Good artists copy, great artists steal.
-Picasso
|
|
Top
|
|
|
|
#19181 - 10/13/04 11:48 AM
Re: Second Java Program (what u think ?)
|
UGN News Staff
Registered: 11/29/02
Posts: 1146
Loc: Canada
|
Agreed :] PHP is pretty sweet especially when combined with MySQL Anyways, I've been working with Java, we learning Constructors and Objects :] Heres a simple program if ur Intrested. The Class: class Circle {
private double r;
public Circle() {
r = 1.0;
}
public Circle(double radius) {
r = radius;
}
public double getr() {
return r;
}
public void setr(double theNewRadius) {
r = theNewRadius;
}
public double theArea() {
return r *= 3.14;
}
}And the Program that uses the class: public class theProgram {
public static void main(String[] args) {
// Using the Defualt Constructer
Circle FirstCircle = new Circle();
System.out.println("(1)The Radius is: " + FirstCircle.getr());
System.out.println("(1)The Area is: " + FirstCircle.theArea());
// Using the Second Constructor (Assignment)
Circle SecondCircle = new Circle(4.1);
System.out.println("(2)Radius is: " + SecondCircle.getr());
System.out.println("(2)The Area is: " + SecondCircle.theArea());
// Using the setr to assign the raduis to the circle
Circle ThirdCircle = new Circle();
ThirdCircle.setr(8.0);
System.out.println("(3)Radius is: " + ThirdCircle.getr());
System.out.println("(3)The Area is:" + ThirdCircle.theArea());
}
}Simple, but hey, its a learning process.
_________________________
Good artists copy, great artists steal.
-Picasso
|
|
Top
|
|
|
|
#19182 - 10/14/04 10:01 AM
Re: Second Java Program (what u think ?)
|
UGN Super Poster
Registered: 06/16/03
Posts: 807
Loc: Wisconsin
|
Agreed, PHP is a powerful, and awesome language. It's also pretty easy to learn.
|
|
Top
|
|
|
|
#19183 - 10/24/04 03:53 AM
Re: Second Java Program (what u think ?)
|
UGN News Staff
Registered: 11/29/02
Posts: 1146
Loc: Canada
|
Agreed, PHP is a powerful, and awesome language. It's also pretty easy to learn. Only problem is that only small and medium sized buisness use PHP/MySQL. The big enterprise buisness dont bother using PHP/MySQL :[
_________________________
Good artists copy, great artists steal.
-Picasso
|
|
Top
|
|
|
|
#19184 - 02/23/05 12:02 PM
Re: Second Java Program (what u think ?)
|
Junior Member
Registered: 02/23/05
Posts: 6
Loc: Malaysia
|
I tried compiling the first java code u post. But some error came out. "cannot resolve symbol variable in".I see that u used in.getChar() so I think u need to import java.io.*; and declared in as BufferedReader... Btw despite what I'm suggesting here, does u program work? 
_________________________
""Don't go looking for heroes to believe in... believe in yourselves." - Roadhandler
|
|
Top
|
|
|
|
|
Registered: 03/07/02
Posts: 270
|
|
2198 Members
46 Forums
24781 Topics
59951 Posts
Max Online: 1567 @ 04/25/10 10:20 AM
|
|
|
0 registered (),
302
Guests and
296
Spiders online. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|