Results 1 to 20 of 20
- 05-03-2011, 11:43 PM #1
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Help with creating an instance of a class extension
Finished product
Java Code://Name: Rob H //Date: 05/03/2011 //Desc: FINAL import java.text.DateFormat; import java.text.SimpleDateFormat; //remove? no, bugs out. import java.util.*; import javax.swing.*; public class AtmHerman { public static void main(String[] args) { int cleared = 0; //Have to use two arrays for this. int[] acc = {0,11112222, 22223333, 33334444}; int[] pin = {0,1111, 2222, 3333}; //Information on accounts String[] name = {"0", "Curly Brace", "Adam Sandler", "Kvothe"}; double[] balance = {0, 100.01, 200.02, 300.03}; //Ask user for account num "insert card" String input1 = JOptionPane.showInputDialog("Please insert card (8 numbers): "); int convert1 = Integer.parseInt(input1); //Now that we have a number, compare it to acc[] int position = 0; for (int i = 0; i < 4; i++) { if (convert1 == acc[i]) { position = i; } else { //nothing } } //If position = 0, end program. if (position == 0) { System.out.println("Account number invalid. Please contact your bank."); System.exit(0); } //Ask for pin. String input2 = JOptionPane.showInputDialog("Please enter pin (4 numbers): "); int convert2 = Integer.parseInt(input2); //Loop. If it fails, close the program. int Three_Strikes = 0; for (int lol = 0; cleared < 1; lol++) { if (convert2 == pin[position]) { cleared = 1; } else { Three_Strikes = Three_Strikes + 1; if (Three_Strikes == 3) { System.exit(0); } input2=JOptionPane.showInputDialog("PIN does not match. Please try again."); convert2 = Integer.parseInt(input2); } } int c = pin[position]; int d = acc[position]; //Create the object CheckingAccountCustomer cust = new CheckingAccountCustomer(name[position], c, balance[position], d); //interface String loop = ""; while (loop != "Q") { //Greet, give name and date. What do you want to do? Take input here. loop = JOptionPane.showInputDialog(AtmHerman.getDateTime() + "\nWelcome " + cust.getName() + ".\n(D)eposit Money\n(W)ithdraw Money\n(C)heck Account Balance\n(Q)uit"); loop = loop.toUpperCase(); if (loop.equals("D")) { cust.deposit(); } else if (loop.equals("W")) { cust.withdraw(); } else if (loop.equals("C")) { cust.checkBalance(); } else if (loop.equals("Q")) { System.out.println("Goodbye!"); cust.quit(); } //Exit the program. else System.out.println("Input Error"); } } //Date and time. private static String getDateTime() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(cal.getTime()); } private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm"; } abstract class Customer { //Declare variables. String name = ""; int pin = 0; double balance = 0.0; //Constructor Customer(String n, int p, double b) // no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private' { name = n; pin = p; balance = b; } Customer() { name = ""; pin = 0; balance = 0.0; } //Accessors String getName() { return name; } int getPin() { return pin; } double getBalance() { return balance; } //These don't do anything until they get inherited. abstract void deposit(); // no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private' abstract void withdraw(); abstract void checkBalance(); abstract void quit(); } class CheckingAccountCustomer extends Customer { int accountNo = 0; //Constructor CheckingAccountCustomer(String n, int p, double b, int a)// no access modifiers, because the desired protection level is between protected and private: thus, the default 'package private' { super(n, p, b); accountNo = a; } void deposit() { String input3 = JOptionPane.showInputDialog("How much are you depositing? "); double convert3 = Double.parseDouble(input3); if (convert3 > 0) { balance = balance + convert3; System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance); } else { System.out.println("Invalid entry"); } } void withdraw() { String input4 = JOptionPane.showInputDialog("How much are you withdrawing? "); double convert4 = Double.parseDouble(input4); if(convert4 > 0) { balance = balance - convert4; if(balance < 0) { balance = balance + convert4; System.out.println("Insufficient funds."); } else { System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance); } } else { System.out.println("Invalid entry"); } } void checkBalance() { System.out.println("As of " + CheckingAccountCustomer.getDateTime() + " your balance is " + balance); } void quit() { System.exit(0); } //Date and time. private static String getDateTime() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(cal.getTime()); } private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm"; }Last edited by Inferno719; 05-04-2011 at 03:52 AM.
-
In the future you'll want to post the exact error message and use forum-accepted code tags (see my link below), but I think that your problem with your constructor call is due to the last parameter not being an int:
This constructor has been written to accept a String, int, double, int, and 12341234 is too big (I think) to be an int. What if you try a smaller number?Java Code:CheckingAccountCustomer cust = new CheckingAccountCustomer("Curly Brace", 2222, 100.01, 12341234);
- 05-03-2011, 11:50 PM #3
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
I actually ran into that problem earlier. 8 digits is short enough to fit inside an int. Thanks though! (I did try it anyway, no luck.)
-
- 05-04-2011, 12:04 AM #5
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
It is done.
-
Ah, I see it now! Your problem is that the class doesn't have a constructor of that type and in fact has no constructors at all! (hint: constructors have no return type, not even void).
Your formatted code helped me to see the error. Thanks for fixing it and much luck!
- 05-04-2011, 12:10 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You gave your constructor a return type of void. Constructors don't have return types, they are special.
replace Constructor with the class name.Java Code:public Constrctor(/*arg list*/)
Edit: too slow :(
- 05-04-2011, 12:10 AM #8
You have a return type of 'void' on the constructor. Remove it, constructors have no return type so java doesn't find any constructors matching your pattern.Java Code://Constructor public void CheckingAccountCustomer(String n, int p, double b, int a) { super.createCustomer(n, p, b); accountNo = a; }
- 05-04-2011, 12:12 AM #9
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Really? No void? That... actually, explains a lot... Thanks!
Of course now there are errors everywhere else =( But I'll see what I can do. Much obliged!
- 05-04-2011, 12:18 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you get stuck again please don't hesitate to ask for help.
A constructor is special, it's unique. It uses the new keyword to allocate space on the heap and stores the constructed item(right hand side) there. Then it binds the reference.
This line does 3 things.Java Code:MyClass x = new MyClass();
1.) creates a reference variable of type MyClass and names it x
2.) allocates space for the object to be added to the heap and stores it there
3.) uses the = to link the 2 items so the reference points to the MyClass object on the heap.
Your error came from using the new keyword on a method with a return type, it can only be used on constructors.
- 05-04-2011, 12:56 AM #11
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
@ sunde, quad64bit, Fubarable Thanks =) I didn't realize that constructors couldn't have return types, I thought they had to have a void to designate 'no return'.
I wonder if I can ask for help again, please! I cleared up some of the errors but there appears to be something else wrong with the constructor. It appears to be constructed normally to me?
- 05-04-2011, 12:57 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Since the constructor is not a method super.Something is wrong. You can just use
to refer to the super classes constructor as the first line of your constructor. This is something the compiler does as well if there is a default constructor. Try this to seeJava Code:super(/*arg list*/);
Java Code:class Test{ public Test(){ System.out.println("Test constructor"); } } public class T extends Test{ public T(){ System.out.println("T Constructor"); } public static void main(String[] args){ T t = new T(); } }
- 05-04-2011, 01:01 AM #13
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
I can't believe I did that -_- It was even written like that in my examples. Thank you again!!
- 05-04-2011, 01:09 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are welcome. If you didn't run my small snippet I suggest you do just to see how the compiler handles it when you don't call super. If you run into more problems don't edit your original post. Instead include the error message and question in the reply to this thread.
- 05-04-2011, 01:10 AM #15
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
I feel kind of bad asking for help again. I swear I was doing well up until now and now nothing wants to work.
I'm trying to call the extended class's methods from the main (deposit, withdraw, check balance) but something's not right with it.
EDIT: Okay, i won't. Should I just change the code, or repost my code if I have another problem??
New error log:
Java Code:----jGRASP exec: javac -g AtmHerman.java AtmHerman.java:122: cannot find symbol symbol : variable cust location: class AtmHerman cust.deposit(); ^ AtmHerman.java:128: cannot find symbol symbol : variable cust location: class AtmHerman cust.withdraw(); ^ AtmHerman.java:134: cannot find symbol symbol : variable cust location: class AtmHerman cust.checkBalance(); ^ 3 errors ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete
Last edited by Inferno719; 05-04-2011 at 01:14 AM.
- 05-04-2011, 01:14 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Don't feel bad, sometimes you just hit a wall and only get frustrated when you continue trying. The object cust is local to main. Meaning it doesn't exist in your helper methods.
That's all I will say to leave it slightly vague, if you still can't understand it please let me know.
- 05-04-2011, 01:18 AM #17
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Oh, its local? I'll have to figure out how to un-local it.
....That also explains why my last project didn't function.
- 05-04-2011, 01:24 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Local is scope of a variable. To break it down as simply as possible, it only lasts until it's correct ending curly brace. I will show you some quick examples which I hope will help you out.
So your customer object exists only in the main method, and it's scope may be smaller than that even depending on where it was declared. You can fix the problem two easy ways(one is to directly call the function instead of using a small helper function. However; I'd like you to try and figure out the other way to fix the problem.Java Code:public int method(){ int x; //local to this method. It can be used anywhere in the method //but no where outside of this method. for(int i = 0; i < 5; ++i){ //here i is local to this loop, ie, it isn't accessible //outside of this loop int z = i + 2 * 5; here z is local to the loop as well. It won't exist outside of it. }//z and i are destroyed here. }//x is destroyed here.
When you have a new problem, edit the original post with the new code, but post error messages in a fresh post.
- 05-04-2011, 03:45 AM #19
Member
- Join Date
- May 2011
- Posts
- 14
- Rep Power
- 0
Thanks, and one more favor?
First of all, thank you for all the help you've given me everyone =) It has been a HUGE help, I don't think my code would be ready to turn in on time without you.
[Java] Java Finished! - Pastebin.com
This is a link to my code (for easier viewing, it spaces very nicely.) I've also updated the code in the thread.
Last favor to ask, i promise. If anyone has spare time, can you run it and tell me if anything goes wrong?
Known issues (I don't have time to fix):
1) Subtracting oddity when withdrawing.
2) Cancel button crashes it kind of.
3) Entering letters crashes the program.
@sunde887, Sorry I didn't have time to figure out how to un-local my class object, I'll try to figure it out after finals =)Last edited by Inferno719; 05-04-2011 at 03:48 AM. Reason: @sunde
- 05-04-2011, 03:53 AM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Make sure you do because if the method was much longer it would be much easier to just have a method and then call it.
Similar Threads
-
Class Instance initialization fails
By JOHNINALBANY in forum New To JavaReplies: 3Last Post: 07-08-2012, 07:43 PM -
How to Instance and reference a class by its name????
By lrocha in forum Advanced JavaReplies: 3Last Post: 12-08-2010, 10:28 PM -
How to check instance of a generic class?
By chan_nguyen in forum New To JavaReplies: 7Last Post: 09-08-2010, 04:20 AM -
get java.lang.NullPointerException while creating an instance
By Basit56 in forum New To JavaReplies: 10Last Post: 01-06-2010, 08:33 AM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks