Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with ArrayList

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with ArrayList

    Hello evryone I'm having trouble undertstandoing how to use arraylist. Here is what i have to do:

    You are to write a program to determine the daily temperature average and keep a running average for up to one week. You will record the temperatures for each day at 6am, noon, and 6pm. The daily average will be the average of these three temperatures. The running average will be the average of the daily averages. You need to be able to print the temperatures and the average for each day. The temperatures will be given in Fahrenheit and should fall between -70 and 130 inclusive.

    -------------------------------------------------------------------------------

    So far i have created my classes but I do not know how to create my ArrayList so i can retrieve an object from the my DailyTemperature class. Also my DailyTemperature class hass two strings, 3 ints, and 1 double

    here is what I got so far from my WeeklyTemp class:

     
    import java.util.ArrayList;
    public class WeeklyTemp {
     
    	ArrayList<DailyTemp> dailyAvgs = new ArrayList<DailyTemp>();
    	DailyTemp obj_1 = new DailyTemp();
    	public void add(){
     
    		for(int i =0; i<dailyAvgs.size();i++){
     
    			dailyAvgs.add(obj_1);
    		}
     
     
    	}
     
     
    }

    and here is my DailyTemp class

     
     
    public class DailyTemp {
     
    	private String day, date;
    	private int sixAM, noon, sixPM;
    	private double dailyAvg;
     
    	public DailyTemp(){
     
    		day = "";
    		date = "";
    		sixAM = 0;
    		noon = 0;
    		sixPM = 0;
    		dailyAvg = 0.0;
     
    	}
     
    	public DailyTemp(String day, String date, int sixAM,
    			int noon, int sixPM, double dailyAvg ){
     
    		this.day = day;
    		this.date = date;
    		this.sixAM = sixAM;
    		this.noon = noon;
    		this.sixPM = sixPM;
    		this.dailyAvg = dailyAvg;
    	}
     
    	public String getDay() {
    		return day;
    	}
     
    	public void setDay(String day) {
    		this.day = day;
    	}
     
    	public String getDate() {
    		return date;
    	}
     
    	public void setDate(String date) {
    		this.date = date;
    	}
     
    	public int getSixAM() {
    		return sixAM;
    	}
     
    	public void setSixAM(int sixAM) {
    		this.sixAM = sixAM;
    	}
     
    	public int getNoon() {
    		return noon;
    	}
     
    	public void setNoon(int noon) {
    		this.noon = noon;
    	}
     
    	public int getSixPM() {
    		return sixPM;
    	}
     
    	public void setSixPM(int sixPM) {
    		this.sixPM = sixPM;
    	}
     
    	public double getDailyAvg() {
    		return dailyAvg;
    	}
     
    	public void setDailyAvg(double dailyAvg) {
    		this.dailyAvg = dailyAvg;
    	}
     
    	@Override
    	public String toString() {
    		return "DailyTemp [day=" + day + ", date=" + date + ", sixAM=" + sixAM
    				+ ", noon=" + noon + ", sixPM=" + sixPM + ", dailyAvg="
    				+ dailyAvg + "]";
    	}
     
     
     
    }


    I will really appreciate any help.. Thanks
    Last edited by copeg; October 26th, 2010 at 08:29 PM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Problem with ArrayList

    I would presume your WeeklyTemp class would like to hold a DailyTemp for each day of the week...so your ArrayList would hold as many for each day of the week. Your add function may be better off accepting a parameter of DailyTemp, in which case you add this parameter to the List.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with ArrayList

    is this what you mean?

    [highlight = java]
    import java.util.ArrayList;
    public class WeeklyTemp {

    private double runningAvgs;

    ArrayList<DailyTemp> dailyAvgs = new ArrayList<DailyTemp>();
    DailyTemp daily_obj = new DailyTemp();
    public void add(DailyTemp t){

    for(int i =0; i<dailyAvgs.size();i++){

    dailyAvgs.add(t);
    }

    }

    }
    [/highlight]

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 03:44 AM
  2. ArrayList Problem
    By Marty in forum Collections and Generics
    Replies: 16
    Last Post: August 31st, 2010, 03:47 AM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM

Tags for this Thread