3 Replies - 1136 Views - Last Post: 09 December 2007 - 07:44 AM Rate Topic: -----

#1 JBailey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-December 07

Write a program to determine the day of the week on which most childre

Post icon  Posted 09 December 2007 - 06:09 AM

Apologies, pretty new to Java programming. I have been asked to write a program regarding the above in Netbeans, this is what I have done so far but I am struggling;


public class Wk8Tut2 {
	   import java.util.Scanner; 

	private static int dayMostBorn = 1;
	private static int day;

	public static void main(String[] args) {
	
	Scanner input = new Scanner(System.in);
		  int weekDaysCount[] = new int[20];
		  int theday = 0;
		
for (int counter=1; counter<20; counter++){ 

System.out.println("Input the day of the week that the child was born");
theday = input.nextInt();

weekDaysCount[theday -1]++;

dayMostBorn = weekDaysCount[0];

//System.out.println("This is true");
for (int day=2; day<7; day++){

if (weekDaysCount [ day -1 ] > dayMostBorn){
dayMostBorn = weekDaysCount [ day -1 ];
dayMostBorn = day;
}//end for

}//end for

}//end if

 System.out.println("The day most children were born on was " + day);

  }//end main
	
}//end class



Output;

Input the day of the week that the child was born
Input the day of the week that the child was born
1
2
Input the day of the week that the child was born
3
Input the day of the week that the child was born
2
Input the day of the week that the child was born
3
Input the day of the week that the child was born
2
Input the day of the week that the child was born
3
Input the day of the week that the child was born
2
Input the day of the week that the child was born
3
Input the day of the week that the child was born
2
Input the day of the week that the child was born
3
Input the day of the week that the child was born
2
Input the day of the week that the child was born
1
Input the day of the week that the child was born
2
Input the day of the week that the child was born
1
Input the day of the week that the child was born
4
Input the day of the week that the child was born
3
Input the day of the week that the child was born
2
Input the day of the week that the child was born
1
The day most children were born on was 0

Any help would be appreciated! Thanks Jo

*1lacca: inserted code tags

Is This A Good Question/Topic? 0
  • +

Replies To: Write a program to determine the day of the week on which most childre

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5820
  • View blog
  • Posts: 12,674
  • Joined: 16-October 07

Re: Write a program to determine the day of the week on which most childre

Posted 09 December 2007 - 06:36 AM

You seem to mostly have this, but there's some confusion on counts and what day you're looking at.

Here's a working version:
public static void findMostBorn() {
	Scanner input = new Scanner(System.in);
	
	// keep a running total of each day
	// this defines an array 0..6.
	// We'll have to translate 1..7 to 0..6 and back again, keep in mind.
	int weekDaysCount[] = new int[7];
	
	// all days should be zero, but doesn't hurt to clear them.
	for (int day=0; day<7; day++) { weekDaysCount[day] = 0; }
	for (int counter=1; counter<20; counter++) {
		// note, if the user enters anything other than 1-7 it will fail
		// you could check for this...
		System.out.println("Input the day of the week that the child was born");
		int theDay = input.nextInt();
		// note, adjust from 1..7 to 0..6 with theDay - 1
		weekDaysCount[theDay - 1]++;
	}
	
	int dayMostBorn = 0;
	for (int day=1; day<7; day++){
		// we're comparing counts and then recording the day number if it's larger
		if (weekDaysCount[day] > weekDaysCount[dayMostBorn]) {
			dayMostBorn = day;
		}
	}
	// dont forget to adjust from 0..6 to 1..7
	dayMostBorn++;
	System.out.println("The day most children were born on was " + dayMostBorn);
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 JBailey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-December 07

Re: Write a program to determine the day of the week on which most childre

Posted 09 December 2007 - 07:35 AM

View Postbaavgai, on 9 Dec, 2007 - 06:36 AM, said:

You seem to mostly have this, but there's some confusion on counts and what day you're looking at.

Here's a working version:
public static void findMostBorn() {
	Scanner input = new Scanner(System.in);
	
	// keep a running total of each day
	// this defines an array 0..6.
	// We'll have to translate 1..7 to 0..6 and back again, keep in mind.
	int weekDaysCount[] = new int[7];
	
	// all days should be zero, but doesn't hurt to clear them.
	for (int day=0; day<7; day++) { weekDaysCount[day] = 0; }
	for (int counter=1; counter<20; counter++) {
		// note, if the user enters anything other than 1-7 it will fail
		// you could check for this...
		System.out.println("Input the day of the week that the child was born");
		int theDay = input.nextInt();
		// note, adjust from 1..7 to 0..6 with theDay - 1
		weekDaysCount[theDay - 1]++;
	}
	
	int dayMostBorn = 0;
	for (int day=1; day<7; day++){
		// we're comparing counts and then recording the day number if it's larger
		if (weekDaysCount[day] > weekDaysCount[dayMostBorn]) {
			dayMostBorn = day;
		}
	}
	// dont forget to adjust from 0..6 to 1..7
	dayMostBorn++;
	System.out.println("The day most children were born on was " + dayMostBorn);
}



Hope this helps.



View PostJBailey, on 9 Dec, 2007 - 07:34 AM, said:

View Postbaavgai, on 9 Dec, 2007 - 06:36 AM, said:

You seem to mostly have this, but there's some confusion on counts and what day you're looking at.

Here's a working version:
public static void findMostBorn() {
	Scanner input = new Scanner(System.in);
	
	// keep a running total of each day
	// this defines an array 0..6.
	// We'll have to translate 1..7 to 0..6 and back again, keep in mind.
	int weekDaysCount[] = new int[7];
	
	// all days should be zero, but doesn't hurt to clear them.
	for (int day=0; day<7; day++) { weekDaysCount[day] = 0; }
	for (int counter=1; counter<20; counter++) {
		// note, if the user enters anything other than 1-7 it will fail
		// you could check for this...
		System.out.println("Input the day of the week that the child was born");
		int theDay = input.nextInt();
		// note, adjust from 1..7 to 0..6 with theDay - 1
		weekDaysCount[theDay - 1]++;
	}
	
	int dayMostBorn = 0;
	for (int day=1; day<7; day++){
		// we're comparing counts and then recording the day number if it's larger
		if (weekDaysCount[day] > weekDaysCount[dayMostBorn]) {
			dayMostBorn = day;
		}
	}
	// dont forget to adjust from 0..6 to 1..7
	dayMostBorn++;
	System.out.println("The day most children were born on was " + dayMostBorn);
}



Hope this helps.


Thank you so much!!!!! Kind Regards Joanna Bailey (UK)
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Write a program to determine the day of the week on which most childre

Posted 09 December 2007 - 07:44 AM

JBailey, welcome to DIC, but next time you post source code, please use code tags like this: :code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1