View Full Version : Calling all Java gurus
deliveryguy
08-13-2008, 12:39 AM
Well, I've got about 3 weeks to brush up on my Java, and I was wondering if anyone here feels like making an assignment and critiquing my code. I've only had an intro course, so start easy! :biggrin:
Goldie
08-13-2008, 01:15 AM
:moved: to the lounge ;)
wadespencer99
08-13-2008, 02:03 AM
Write a script that automatically moves every thread out of the geninfo section...:rofl:
deliveryguy
08-13-2008, 02:06 AM
Hahah - sorry boot that guise :facepalm:
wadespencer99
08-13-2008, 02:22 AM
No.....I'm serious :)
FastCougar
08-13-2008, 02:24 AM
Write the infamous "Bank ATM" program. Pretend you are a patron at a bank ATM. The program should be able to check your balance, dispense cash, transfer funds and receive deposits ... you get the idea! Oh yeah, and don't forget that there is an authentication scheme in there involving you entering a card, so the card must be read and then the PIN verified. Also, don't store the PIN on the card ... the program must make an external call for PIN verification. Enjoy!!!
P.S. this is an actual assignment from introduction to computer programming ;)
deliveryguy
08-13-2008, 02:26 AM
Bwaha unfortunately I am unacquainted with using java to manipulate teh interwebz.
If all threads were exported to an array, I could route threads with certain parameters to another array (or just do it manually). Hopefully this year I'll learn what I need to do some internet application.
deliveryguy
08-13-2008, 02:26 AM
Oooh thanks man - I think I can do that one. :biggrin:
FastCougar
08-13-2008, 02:27 AM
Hopefully this year I'll learn what I need to do some internet application.Do yourself a favor ... learn ColdFusion!
deliveryguy
08-13-2008, 02:59 AM
Alright - this is what I've got done so far:
public class BankAccount{
int AccountNumber;
int PIN;
double AccountBalance;
public static void main (String args []){
BankAccount FastCougar = new BankAccount();
BankAccount Desolator = new BankAccount();
FastCougar.setAccountNumber(4326262012345678);
FastCougar.setAccountBalance(2318726.39);
Desolator.setAccountNumber(4326262098765432);
Desolator.setAccountBalance(25.00);
System.out.print("Please enter your name: ");
String User = Scan.nextln();
if (User = "FastCougar" || "Fastcougar" || "fastcougar"){
FastCougar.Verify()
}
}
void setAccountBalance(double Balance){
AccountBalance = Balance;
}
void setAccountNumber(int Number){
AccountNumber = Number;
}
void Verify(){
System.out.println("Please enter your account number: ");
int checknum = Scan.nextInt();
if checknum = FastCougar.
}
Here's the plan - once the "verify" method checks out, I'll make a menu selection using try/catch...or maybe just a bunch of ifs.
One issue -
if checknum = FastCougar.xxxxxx
I'm REALLY rusty on my data calls. I'm trying to reference FastCougar's account number here, and check it against the "checknum" entry. I'm pretty sure the dot operator shouldn't be used; any suggestions?
FastCougar
08-13-2008, 03:11 AM
When was the last time you inserted your ATM card into an ATM machine and where asked to enter your name or account nubmer? KISS (keep it simple stupid)
FastCougar
08-13-2008, 03:12 AM
You might be better served to flowchart the process before coding ;)
deliveryguy
08-13-2008, 03:13 AM
:rofl: Yes sir!
deliveryguy
08-13-2008, 03:38 AM
http://i180.photobucket.com/albums/x218/carnage2048/flow-1.jpg
There's the first half...I'm tiiired. This is sort of embarrassing; it's amazing how much I forgot in 2 months. :banghead:
LinkMan
08-13-2008, 09:29 AM
Write the infamous "Bank ATM" program. Pretend you are a patron at a bank ATM. The program should be able to check your balance, dispense cash, transfer funds and receive deposits ... you get the idea! Oh yeah, and don't forget that there is an authentication scheme in there involving you entering a card, so the card must be read and then the PIN verified. Also, don't store the PIN on the card ... the program must make an external call for PIN verification. Enjoy!!!
P.S. this is an actual assignment from introduction to computer programming ;)
And don't forget to take into account the multiple access issues inherent in this problem -- what happens when you check the balance at the same time a transfer or deposit or withdrawal is taking place? What happens when something fails during the transaction - you must be able to roll it back?
In Real Life, this isn't a simple problem. Good luck :)
deliveryguy
08-14-2008, 04:34 PM
Alright - I've got a little more done, but I'm not nearly as comfortable with this as I feel like I should be. Would you guys be alright with me using this as a sort of self-edumacation thread? I'd like to start from square one, and use your input to help. I took an intro course last year, but I wasn't studious by any means, and I want to understand EXACTLY how things work...not just "barely understand", you know? It would be like...a little log of my progress. I know this isn't a Java forum, but any input along the way would be fantabulous. If it's alright with teh community, i can haz java?
deliveryguy
08-16-2008, 09:42 PM
Alright! Here it goes. I'm using a few different sources to form a pretty comprehensive review.
~~ Java Review: Day 1 ~~
What makes Java special? Platform independence - the code's ability to be transferable to other platforms without having to re-write the code to work with the individual platform. This makes Java a powerful tool for the internet - Mac users, linux users, and Windows machines, all with different OS kernel, each have their own JVM (Java Virtual Machine) to run the compiled code. The JVM converts the bytecode of the compilation into machine-specific code. When you download a Java extension for firefox, the web browser is equipped with its own internal JVM, or bytecode interpreter.
Code » Compiler » Bytecode (Platform independent) » Interpreter
A disadvantage to using Java is speed - instead of going through one level of compilation like traditional programming languages, Java requires the interpretation of already compiled code. This shouldn't be much of an issue with today's computers.
Java is object-oriented. This is a concept that I *sort of* understand. I'm hoping my comfort-level with the idea will grow with time.
The nature of Java allows a programmer to build individual, independent "components", or objects, that form a large-scale application. The way that these objects interact with eachother can be modified.
When you make an object in Java, the object is a concrete representation of a class. A class could be something like...a computer. Within that class there are features; what kind of computer? What processor speed? How much RAM? What color is the case? Those would be attributes, while there are also actions that the computer could perform. These features would be the behavior of the object - print the paper, browse the internet, crash randomly.
Instance variables define the attributes of an object. Their names are defined in the class, while their information is defined in the object. The attributes can also be changed in the object. These variables can be constant or dynamic.
A class's behavior is defined/modified through methods. In order for an action to take place, methods must exist. Classes and objects can communicate with other classes and objects, using their methods or gaining their attributes. (I don't recall going to class when Inheritance was discussed...I'm not too sure about inheriting instance variables).
class Computer{ Class definition
String Manufacturer; creates a new string object - a string is NOT one of the 8 primitive data types
String CaseColor;
int RAMGB;
int HDD;
int GFX;
double CPU;
boolean dualcore;
boolean crash;
boolean booted;
boolean printing;
boolean internet;
These are instance variables.
void Boot(){
if (booted == true){
System.out.println("The computer is already on you bafoon");
else {
booted = true;
System.out.println ("Your computer is now on");
}
}
This is the Boot() method. Void means that the method does not return any information to the main method - correct? The parentheses append every method definition; if the method accepts data from the calling method, the data's information is placed between the parentheses. Correct?
So this would be a chunk of the computer program. Every program needs a "main method" to drive the methods and tell the program what to do. We could add any number of additional methods to expand the program's capability. For instance:
void DisplayRamSpecs(){
System.out.println ("Your computer is equipped with " + RAMGB + " Gigs of RAM."
}
SO - since we have defined a class (computer), attributes (specs), and behavior (boot, DisplayRamSpecs, anything else), we have a nice template from which we can make objects. The objects we will be making are...computers! I get it!
In order to create an object, we need to create that main() method. Something like this:
public static void main(String args[]){
Computer Jack = new Computer();
Computer Jill = new Computer();
This just created two objects - two instances of the computer class. Jack and Jill are now equipped with two separate computers - each with their own attributes, but each able to perform the same action (method) of booting up, displaying RAM specs, or any other method we choose to create.
In order to modify the attributes of each person's computer, the dot operator is used.
Jack.CaseColor = "purple and green";
Jack.RAMGB = 2; has to be an INTEGER
Jack.HDD = 1500;
Jack.GFX = 256;
Jack.CPU = 2.66;
Jack.dualcore == true;
Jack.crash == false;
Jack.booted == false;
Jack.printing == false;
Jack.internet == false;
This created Jack's computer. Now, we created a method (outside the main method) that turned the computer on. We could create another method that changes the paint color. or another that installs more RAM. Here's one that connects him to the internwebs and reports a crash:
void Porn(){
internet == true;
System.out.println ("You are connected to the internet.");
We could insert a random number generator here, and depending on odd/even, or some other algorithm, we could have the following result take place:
crash == true;
System.out.println ("You should get out more.");
booted = false;
}
Back to our main method, the one that drives the programs, and calls the pron and boot methods. I think of methods as activities...actions.
This watermelon is SO good.
public static void main(String args[]){
Computer Jack = new Computer();
Computer Jill = new Computer();
Jack.CaseColor = "purple and green";
Jack.RAMGB = 2; has to be an INTEGER
Jack.HDD = 1500;
Jack.GFX = 256;
Jack.CPU = 2.66
Jack.dualcore == true;
Jack.crash = false;
Jack.booted = false;
Jack.printing = false;
Jack.internet = false;
Jack.Boot();
Jack.Porn();
The main method left Jill's computer blank; while her computer is an object, her instance variables are all set to default 0. What's the default for boolean? The main method simulated Jack's lonely Saturday night; turning on his computer, watching porn, and potentially crashing his computer.
Final program:
class Computer{
String Manufacturer;
String CaseColor;
int RAMGB;
int HDD;
int GFX;
double CPU;
boolean dualcore;
boolean crash;
boolean booted;
boolean printing;
boolean internet;
public static void main(String args[]){
Computer Jack = new Computer();
Computer Jill = new Computer();
Jack.CaseColor = "purple and green";
Jack.RAMGB = 2;
Jack.HDD = 1500;
Jack.GFX = 256;
Jack.CPU = 2.66;
Jack.dualcore = true;
Jack.crash = false;
Jack.booted = false;
Jack.printing = false;
Jack.internet = false;
Jack.DisplayRamSpecs();
Jack.Boot();
Jack.Porn();
}
void DisplayRamSpecs(){
System.out.println ("This computer is equipped with " + RAMGB + " Gigs of RAM.");
}
void Boot(){
if (booted == true){
System.out.println("The computer is already on you bafoon");
}
else {
booted = true;
System.out.println ("Your computer is now on");
}
}
void Porn(){
internet = true;
System.out.println ("You are connected to the internet.");
crash = true;
System.out.println ("You should get out more.");
booted = false;
}
}
Output:
This computer is equipped with 2 Gigs of RAM.
Your computer is now on
You are connected to the internet.
You should get out more.
COOGAH
08-17-2008, 12:21 AM
i have a simpler solution:
[calls up someone in India] here, do this for me, kthxbye [click]
done!
deliveryguy
09-05-2008, 08:06 PM
Mmmk so I was screwing around with some methodologies and I'm stuck. This is what I've got:
import java.util.Scanner;
public class Project1 {
Scanner scan = new Scanner(System.in);
public static void main(String[] args){
double Double = 0;
int Integer = 0;
String c = null;
Project1 example = new Project1();
example.getDoub();
example.getInt();
example.concat(Double, Integer);
example.print(c);
}
public double getDoub(){
System.out.println ("Type the double: ");
double Double = scan.nextDouble();
return Double;
}
public int getInt(){
System.out.println("Type the integer: ");
int Integer = scan.nextInt();
return Integer;
}
public String concat(double d, int i){
String c = d + "/t" + i;
return c;
}
public void print (String subC){
System.out.println("The concateation is: " + subC );
}
}
So basically it's a simple concatenation program and I'm screwing around with method calls and data sharing; here's an extremely simple solution:
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a double number");
double d1 = scan.nextDouble();
System.out.print("Enter an integer number");
int i1 = scan.nextInt();
String result = d1 + "\t" + i1;
System.out.println("You entered: " + result);
So with the first program, does anyone see where the data passing is fudged up?