Results 1 to 20 of 20
  1. #1
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:

    Java Code:
    CheckingAccountCustomer cust = new CheckingAccountCustomer("Curly Brace", 2222, 100.01, 12341234);
    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?

  3. #3
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    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.)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Inferno719 View Post
    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.)
    Sorry about the bad advice. Then you'll want to post the exact error message. Also, you'll want to edit your original post and add proper code tags, that look like

    [code] -- above your code block

    and

    [/cod&#101;] -- below your code block

  5. #5
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    It is done.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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!

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You gave your constructor a return type of void. Constructors don't have return types, they are special.

    Java Code:
    public Constrctor(/*arg list*/)
    replace Constructor with the class name.

    Edit: too slow :(

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Java Code:
    //Constructor
    	public void CheckingAccountCustomer(String n, int p, double b, int a)
    	{
    		super.createCustomer(n, p, b);
    		accountNo = a;
    	}
    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.

  9. #9
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    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!

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

    Java Code:
    MyClass x = new MyClass();
    This line does 3 things.
    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.

  11. #11
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    @ 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?

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since the constructor is not a method super.Something is wrong. You can just use
    Java Code:
    super(/*arg list*/);
    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 see

    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();
      }
    }

  13. #13
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    I can't believe I did that -_- It was even written like that in my examples. Thank you again!!

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  15. #15
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    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.

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  17. #17
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    Oh, its local? I'll have to figure out how to un-local it.

    ....That also explains why my last project didn't function.

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

    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.
    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.


    When you have a new problem, edit the original post with the new code, but post error messages in a fresh post.

  19. #19
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default 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

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  1. Class Instance initialization fails
    By JOHNINALBANY in forum New To Java
    Replies: 3
    Last Post: 07-08-2012, 07:43 PM
  2. Replies: 3
    Last Post: 12-08-2010, 10:28 PM
  3. How to check instance of a generic class?
    By chan_nguyen in forum New To Java
    Replies: 7
    Last Post: 09-08-2010, 04:20 AM
  4. Replies: 10
    Last Post: 01-06-2010, 08:33 AM
  5. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •