Results 1 to 4 of 4
  1. #1
    Bakaouji is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Parallel arrays and classes

    I have a class account, with id, balance, and interest rate, which I can set using their set method.

    In the last question of my homework, my teacher asked me to store the above data in parallel arrays, and then display only after an ID of 9999 is entered.

    I know how parallel arrays work, but I'm not sure what's the best way of working with them & classes.

    Should I create arrays in my main and store there? Or should I do it in the class (if so how) ?

    My void main has this:
    Java Code:
    		
    		System.out.println("Enter an account ID");
    		MyAccount.setid(Input.nextInt());
    		
    		while(MyAccount.getid() != 9999)
    		{
    			System.out.println("Enter an account balance");
    			MyAccount.setbalance(Input.nextDouble());
    			
    			System.out.println("Enter an annual interest rate");
    			MyAccount.setannualInterestRate(Input.nextDouble());
    			
    			System.out.println("Enter an account ID");
    			MyAccount.setid(Input.nextInt());
    		}

    And my class Account:

    Java Code:
    import java.util.Date;
    
    public class Account 
    {
    	private Date dateCreated = new Date();
    	private int id = 0;
    	private double balance = 0;
    	private double annualInterestRate = 0;
    	
    	 Account()
    	 {
    		 this.id = id;
    		 this.balance = balance;
    		 this.annualInterestRate = annualInterestRate;
    		 this.dateCreated = dateCreated;
    	 }
    	 
    	 Account(int id, double balance, double annualInterestRate)
    	 {
    		 this.id = id;
    		 this.balance = balance;
    		 this.annualInterestRate = annualInterestRate;
    	 }
    	
    	int getid()
    	{
    		return id;
    	}
    	
    	double getbalance()
    	{
    		return balance;
    	}
    	
    	double getannualInterestRate()
    	{
    		return annualInterestRate;
    	}
    	
    	void setid(int id)
    	{
    		this.id = id;
    	}
    	
    	void setbalance(double balance)
    	{
    		this.balance = balance;
    	}
    	
    	void setannualInterestRate(double annualInterestRate)
    	{
    		this.annualInterestRate = annualInterestRate;
    	}
    	
    	Date getdateCreated()
    	{
    		return dateCreated;
    	}
    	
    	void withdraw(double amount)
    	{
    		balance = balance - amount;
    	}
    	
    	void deposit(double amount)
    	{
    		balance = balance + amount;
    	}
    	
    	double getMonthlyInterestRate()
    	{
    		double monthly;
    		
    		monthly = annualInterestRate / 12;
    		
    		return monthly;
    	}
    	
    }
    Please help!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,461
    Rep Power
    25

    Default Re: Parallel arrays and classes

    what's the best way of working with them & classes.
    Are you asking how to get rid of the parallel arrays and move the data into a class? This will simplify your code and make it safer for many operations, like sorting.

    I don't see any arrays in the code you posted. What does your posted code have to do with arrays?

  3. #3
    Bakaouji is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Parallel arrays and classes

    I did it by changing the variables to arrays in the string and editing the rest of the code accordingly.

    My question wasn't clear enough, sorry about that.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,461
    Rep Power
    25

    Default Re: Parallel arrays and classes

    Your posted code looks inefficient. It appears to have created a MyAccount object with an invalid id and then exits the loop.
    It would be better to test the id's value before creating the object with the invalid id.
    I'm assuming that an id of 9999 is invalid.

Similar Threads

  1. parallel arrays
    By belfast09 in forum New To Java
    Replies: 6
    Last Post: 06-13-2011, 11:48 AM
  2. parallel arrays
    By the beginner in forum New To Java
    Replies: 9
    Last Post: 02-22-2011, 05:35 AM
  3. Parallel Arrays
    By mwenchong in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 12:20 AM
  4. two parallel arrays
    By Adomini in forum New To Java
    Replies: 12
    Last Post: 09-07-2010, 01:45 AM
  5. How to create parallel arrays
    By Roselicious in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 12:10 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
  •