GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Feb-2007, 20:43
msm1593 msm1593 is offline
Junior Member
 
Join Date: Jan 2007
Posts: 32
msm1593 is on a distinguished road

Question with accessors


Hey guys, i'm writing a program that creates an of N objects, where N is a number the user inputs. You can then change and access any attributes of the N objects, but I don't want the program to throw an exception and close out when the user elects to change something that doesn't actually exist. For example, creating N objects, then trying to access the N+4th object.

I would like something like the following to work, though i cannot seem to figure out how to make such a thing occur

JAVA Code:
private int getObjectAttribute(int index)
{
   if(index < size)
   return objectList.get(index).getObjectAttribute();
}
Otherwise I would just assume the program not print anything, or maybe throw up an "invalid" message, is that possible at all?

Thanks in advance guys
  #2  
Old 12-Feb-2007, 21:12
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 992
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Question with accessors


Can you post more code for what you have, to get a better picture?
Where is size set?
What if index < 0, then what happens?
Are you getting errors now, if so, what are they? (post them all)

etc...
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 12-Feb-2007, 21:23
msm1593 msm1593 is offline
Junior Member
 
Join Date: Jan 2007
Posts: 32
msm1593 is on a distinguished road

Re: Question with accessors


Absolutely, here is the code in full, i'm working on commenting currently, along with trying to clean it up, so it's a bit messy, but i'm using BlueJ for the program. It's probably not the most robust program as i'm only a few weeks deep into my first java course.


HeaterControl
JAVA Code:

import java.util.ArrayList;

/**
 * Write a description of class heaterControl here.
 * 
 * @author 
 * @version 2.5.07
 */

public class heaterControl
{
// instance variables - various varibles
    private ArrayList<Heater> heaterList;  
    private int size; 
    private int target;
    public int place = 0;
    
    
 /**
 * createHeaters - Creates N heaters
 *
 * @param  size Creates the designated number of heaters
 * 
 */
  
    public void createHeaters(int size)
    {
        this.size = size;
        heaterList = new ArrayList<Heater>(); 
        for (int i = 0; i < size; i++)
        {
            heaterList.add(new Heater(15, 1, 40, 5));
        }
    }
    
/**
 * getHeaterCount - displays how many heaters were created
 *
 * 
 * @return     heaterList.size() (the number of heaters in the array)
 */
    
    public int getHeaterCount()
    {
        return heaterList.size();
    }
    
/**
 * setNight - Sets every heater in the array to it's minimum temperature
 *
 * 
 * 
 */
    
    public void setNight()
    {
        for(int i = 0; i < size; i++)
        {
        heaterList.get(i).setDay(heaterList.get(i).getTemperature());
        heaterList.get(i).setTemperature(heaterList.get(i).getMin());
    }
    }
    
/**
 * setDay - Sets every heater in the array to it's day time temperature
 *
 * 
 * 
 */
    
    public void setDay()
    {
        for(int i = 0; i < size; i++)
        {
            heaterList.get(i).setTemperature(heaterList.get(i).getDay());
        }
       
    }
/**
 * setHeaterTemperature - sets heater X to temperature Y
 *
 * @param index - Designates which heater is to be changed
 * @param temperature - Designates what temperature to set the given heater to
 * 
 */    
        
    public void setHeaterTemperature(int index, int temperature)
    {
        if((heaterList.get(index).getMin() <= temperature) && (heaterList.get(index).getMax() >= temperature))
        heaterList.get(index).setTemperature(temperature);
    }
    
/**
 * setHeaterIncrement - sets heater X to increment Y
 *
 * @param index - Designates which heater is to be changed
 * @param increment - Designates what increment to set the given heater to
 * 
 */    

    public void setHeaterIncrement(int index, int increment)
    {
        heaterList.get(index).setIncrement(increment);
    }

/**
 * getHeaterTemperature - gets the temperature for heater X
 *
 * @param index - Designates which heater is to be returned
 * @return The given heater's temperature
 */    

    public int getHeaterTemperature(int index)
    {
       return heaterList.get(index).getTemperature(); 
    }
    
/**
 * getHeaterIncrement
 *
 * @param  index - Designates which heater is to be returned
 * @return The given heater's increment
 */

    public int getHeaterIncrement(int index)
    {
        return heaterList.get(index).getIncrement();
    }
    
/**
 * removeHeater - user inputs heater X, all heaters above it in the list
 *                are knocked down one level, then the last node is deleted.
 *
 * @param index - Designates which heater is to be deleted
 * 
 */

    public void removeHeater(int index)
    {
        for(int i = index; i < size-1; i++)
        {
            heaterList.get(i).setTemperature(heaterList.get(i+1).getTemperature());
            heaterList.get(i).setMin(heaterList.get(i+1).getMin());
            heaterList.get(i).setMax(heaterList.get(i+1).getMax());
            heaterList.get(i).setIncrement(heaterList.get(i+1).getIncrement());
            heaterList.get(i).setDay(heaterList.get(i+1).getDay());
        }
        heaterList.remove(index+1);
    }
   
/**
 * accessHeater - Creates a reference to heater X
 *
 * @param index - Designates which heater to access
 * @return Heater - returns the attributes of the given heater
 */    

    public Heater accessHeater(int index)
    {
        return heaterList.get(index);
    }
            
/**
 * applyValidChange - accessed from Warmer and Cooler
 *                    applies the change only if it's within proper bounds
 *
 * @param  target - Target temperature to be set
 * 
 */
    
    private void applyValidChange(int target)
    {
        int a = heaterList.get(place).getMax();
        int b = heaterList.get(place).getMin();
        if(target <= a && target >= b)
        {
           heaterList.get(place).setTemperature((heaterList.get(place).getTemperature() + heaterList.get(place).getIncrement()));
        }
        
     }
    
/**
 * warmer - calculates the 'target' which calculates the temperature
 *          for a given heater after adding the increment
 *
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y
 */
     
    public void warmer()
    {
        place = 0;
        for(int i = 0; i < size; i++)
        {
            target = (heaterList.get(i).getTemperature() + heaterList.get(i).getIncrement());
            
            applyValidChange(target);
            place++;
        }
    }
    
    /**
     * method calculating the target temperature given by heaterTemp - heaterIncrement
     * passes the value to appllValidChange for inspection
     */
    public void cooler()
    {
        place = 0;
        for(int i = 0; i < size; i++)
        {
            target = (heaterList.get(i).getTemperature() - heaterList.get(i).getIncrement());
            applyValidChange(target);
            place++;
        }
    }
}


Heater
JAVA Code:
  
/**
 * Class controlling the heat in a given area. 
 * 
 * @author
 * @version 2.5.07
 */
public class Heater 
{

//declare variables

    private int temperature;
    private int min;
    private int max;
    private int increment;
    private int day;

//various accessors, one for each variable
    public Heater(int temperature, int min, int max, int increment)
    {
        this.temperature = temperature;
        this.min = min;
        this.max = max;
        this.increment = increment;
        this.day = temperature;
    }


/**
 * various accessors for variables
 */
    public int getTemperature()
    {
        return temperature;
    }
    
    public int getMin()
    {
        return min;
    }    
    
    public int getMax()
    {
        return max;
    }
    
    public int getIncrement()
    {
        return increment;
    }
    
    public int getDay()
    {
        return day;
    }

    /**
     * various mutators for variables
     */
    
    public void setIncrement(int increment)
    {
        this.increment = increment;
    }
    
    public void setTemperature(int temperature)
    {
        this.temperature = temperature;
    }
    
    public void setDay(int temperature)
    {
        day = temperature;
    }
    
    public void setMin(int min)
    {
        this.min = min;
    }
    
    public void setMax(int min)
    {
        this.max = max;
    }
   
}


The error that pops up is just that i'm trying to access an element of the array that doesn't exist (which i expected to pop up) i'd just rather it didn't pop up. I'm supposed to deal with out of bounds errors "gracefully"
  #4  
Old 12-Feb-2007, 21:30
msm1593 msm1593 is offline
Junior Member
 
Join Date: Jan 2007
Posts: 32
msm1593 is on a distinguished road

Re: Question with accessors


Also, you've made a good point; I'd like index to be within certain bounds

atleast zero would be needed as well as below size
  #5  
Old 13-Feb-2007, 08:32
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 992
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Question with accessors


Do you have a post for the 'driver' file? There's not a main (unless it was taken out during the 'clean-up' ) in either file, so it would be easier work with what you have.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #6  
Old 13-Feb-2007, 12:01
msm1593 msm1593 is offline
Junior Member
 
Join Date: Jan 2007
Posts: 32
msm1593 is on a distinguished road

Re: Question with accessors


There actually is no main :-/, In blueJ i've got to create in instance of heaterControl and test things through that... But i think i have figured it out, i'll post the code later today
  #7  
Old 13-Feb-2007, 13:08
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 992
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Question with accessors


You're right, an instance is all that's needed, but since there isn't any output, I didn't bother trying to 'slap' something together.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Borland compile question monnick C++ Forum 4 12-Feb-2006 18:40
non-member function question crq C++ Forum 1 03-Feb-2005 22:59
Simple question on arrays--please help! brookeville C++ Forum 16 18-Nov-2004 00:23
Repetition structure problem and question brookeville C++ Forum 17 29-Oct-2004 18:48
question of practice magiccreative C++ Forum 1 06-Feb-2004 08:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.