Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 20th, 2006, 05:08 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
Jus need feedback and ideas

We were given a simple program to work with. All we do is have the user choose from a list of presidents and when the user chooses a president it shows all the presidents on the that are before and after the president the user has chosen. very simple....all it requires really is a bunch of if statements. I was jus wondering is their a different way i can do this? My teacher mentioned ordinance power or something i think i did not hear what he said. Hmm jus want feedback on how i should do this. IF you have a way that does not invovle a lot of if statements or none at any jus mention it. if you do have a way and can be linked to a website or so just post. thank you!

Reply With Quote
  #2  
Old November 21st, 2006, 01:11 AM
fizker fizker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: denmark
Posts: 42 fizker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 18 m 18 sec
Reputation Power: 15
Send a message via MSN to fizker
Hi.

As I see it, you can do it in two ways; the object-oriented and the not-quite-so-object-oriented.
There is no need to muck with if-statements in either approach.

The non-oo is the most simple:
Here I assume that you have an array of the presidents, something like:
Code:
String[] presidents = new String[]{"President 1","President 2"}

Then when it is time to display the list, use the selected index of the drop-box to figure which is before, which is chosen and which is after.
I am assuming you use a JComboBox:
Code:
public String showPresidents(JComboBox combobox) {
// combobox is the instance of JComboBox presented to the user // and more important, the combobox is also 0-indexed, // so the index is directly compatible with our array int selected = combobox.getSelectedIndex(); // you could concatenate strings rather than using the StringBuilder, // but this is way more efficient StringBuilder builder = new StringBuilder(); if(selected > 0) builder.append("The following went before:\n"); for(int i = 0; i < selected; i++) {
builder.append(presidents[i]);
} builder.append("The selected president was: "); builder.append(presidents[i]); if(selected+1 < presidents.length) builder.append("The following went after:\n"); for(int i = selected+1; i < presidents.length; i++) {
builder.append(presidents[i]);
} return builder.toString();
}

That is the quick-and-dirty. The oo-way would be to wrap the presidents into an object capable of determining which went before and after. It is a bit more complex and not really necessary here, but if something more specific than a print was in order, it would be better.
I would build it as a linked list, with every president aware of the two surrounding presidents.
__________________
Benjamin Horsleben
horsleben.com/benjamin
Don't blame malice for what stupidity can explain

Last edited by fizker : November 22nd, 2006 at 01:40 PM.

Reply With Quote
  #3  
Old November 21st, 2006, 11:00 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
// combobox is the instance of JComboBox presented to the user
// and more important, the combobox is also 0-indexed,
// so the index is directly compatible with our array
// you could concatenate strings rather than using the StringBuilder,
// but this is way more efficient

K Combobox is basically the introduction? asking the user to choose a president etc..? wat do you mean by setting an index as 0? Should i create a seprate function that list the presidents before and after..im kinda new to this and finding a new way to do a program that ive never done before kind of confusing..explnation and tutorial mayb will help?

Reply With Quote
  #4  
Old November 22nd, 2006, 01:39 PM
fizker fizker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: denmark
Posts: 42 fizker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 18 m 18 sec
Reputation Power: 15
Send a message via MSN to fizker
arrays in java is, as opposed to basic-derived languages, based on 0. that is, the first value in the array is at index=0. this is quite basic stuff. there are tons of introductory tutorials out there, perhaps ask our friendly neighbourhood google about it?

and you should just call the method i wrote with a jcombobox (the standard dropdown box used in the swing frameset) as parameter, and it returns a string of all the presidents, with the selected highlighted.

I just spotted that the method won't compile (i seem to have forgotten to return), so i have edited the code to be functional

Reply With Quote
  #5  
Old November 27th, 2006, 04:25 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
Ok i got a couple questions i know this is simple for you guys but im new to this all your help is much appriciated. Ok, Where should i put the system.out.print to list the presidents and so the user can choose a president? Second How do i use the jcombobox? Third how do you call a function with parameters to the main function? recursion?

Code:
public class Cph
{
	public static void main(String[] args)
	{
		showPresidents();	
	}
	public String showPresidents(JComboBox combobox) 
	{
		String[] presidents = new String[]{"Eisenhower","Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan", "Bush", "Clinton", "BushJr"};
		int selected = combobox.getSelectedIndex();
		StringBuilder builder = new StringBuilder();
	if(selected > 0) 
		builder.append("The following went before:\n");
	for(int i = 0; i < selected; i++) 
		{
			builder.append(presidents[i]);
		}
		builder.append("The selected president was: ");
		builder.append(presidents[i]);
	if(selected+1 < presidents.length) 
		builder.append("The following went after:\n");
	for(int i = selected+1; i < presidents.length; i++) 
		{
			builder.append(presidents[i]);
		}

return builder.toString();
	}
}

Reply With Quote
  #6  
Old November 28th, 2006, 01:38 PM
fizker fizker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: denmark
Posts: 42 fizker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 18 m 18 sec
Reputation Power: 15
Send a message via MSN to fizker
To print to a console, use the System.out print writer instance. There are two methods in particular you are interested in, System.out.print and System.out.println. Both take a variety of different parameter types. print prints whatever you tell it, and println prints what you tell it as well as suffixes a newline.

I have simplified the code a bit, leaving out the jcombobox. I guess it was a bit out of your grasp at this time, and the principle could be done with a console app . The jcombobox requires a jpanel to be embedded in, which is quite advanced compared to your original question.

I have modified the code to require a single argument when running the app, allowing you to select which president to use. Now, it does require the first argument to be an int between 0 and 9, or it will crash.
Code:
public class Cph
{
	public static void main(String[] args)
	{
int selectedPresident = Integer.parseInt(args[0]);
		String[] presidents = new String[]{"Eisenhower","Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan", "Bush", "Clinton", "BushJr"};

if(i < 0 || i >= presidents.length) {
System.out.println("The number must be between 0 and "+(presidents.length-1)+", both inclusive");
exit(0);
}

		System.out.println(showPresidents(presidents, selectedPresident));	
	}
	public String showPresidents(String[] presidents, int selected) 
	{
		StringBuilder builder = new StringBuilder();
	if(selected > 0) 
		builder.append("The following went before:\n");
	for(int i = 0; i < selected; i++) 
		{
			builder.append(presidents[i]);
		}
		builder.append("The selected president was: ");
		builder.append(presidents[i]);
	if(selected+1 < presidents.length) 
		builder.append("The following went after:\n");
	for(int i = selected+1; i < presidents.length; i++) 
		{
			builder.append(presidents[i]);
		}

return builder.toString();
	}
}


i am not entirely sure what you are meaning with the third question.

Reply With Quote
  #7  
Old November 28th, 2006, 08:31 PM
ChaNgeD ChaNgeD is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 15 ChaNgeD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 37 sec
Reputation Power: 0
Thx for all your help got it finnaly running. One problem something wrong with the for loops , i tried to play with it a little still nothing. It does not print out the before list and the after list prints out the same list of presidents evrytime and probaly will for the before presidents also hmmm this last thing i need and im done

Code:
import javax.swing.*;											
public class Cph
{
 public static void main(String[] args)
 {
  int again, Selected = 0;
  do
  {
   JOptionPane.showMessageDialog(null, "-=Welcome=-");
   Object[] Presidents = {"Eisenhower","Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan", "Bush", "Clinton", "BushJr"};			   
   Object SelectedValue = JOptionPane.showInputDialog(null, "Choose a President", "List of Presidents to choose from:",JOptionPane.INFORMATION_MESSAGE, null,Presidents, Presidents[0]); 
   Cph main = new Cph();										 
   System.out.println(main.showPresidents(Presidents,   Selected));							   
   again = JOptionPane.showConfirmDialog (null, "Would you like to try again? "); 		   
  }while (again == JOptionPane.YES_OPTION);
 }
 public static String showPresidents(Object[] Presidents, int Selected) 
 {
  StringBuilder PresChosen = new StringBuilder(); 								   
  if(Selected > 0)
  {
   PresChosen.append("\nPredecessor(Before): "); 								   
  }
  for(int i = 0; i < Selected; i++)
  {
   PresChosen.append(Presidents[i]);									  
  }
  if(Selected+1 < Presidents.length)
  {
   PresChosen.append("\nSuccesor(After): "); 									
  }
  for(int i = Selected+1; i < Presidents.length; i++)
  {
   PresChosen.append(Presidents[i]);									  
  }
  return PresChosen.toString(); 										
 }
}

thx again i learned alot lol.

Reply With Quote
  #8  
Old December 5th, 2006, 12:17 AM
fizker fizker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: denmark
Posts: 42 fizker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 18 m 18 sec
Reputation Power: 15
Send a message via MSN to fizker
I tried your code, and I think I found the problem. I have highlighted the important parts below
Code:
import javax.swing.*;											
public class Cph
{
 public static void main(String[] args)
 {
  int again, Selected = 0;
  do
  {
   JOptionPane.showMessageDialog(null, "-=Welcome=-");
   Object[] Presidents = {"Eisenhower","Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan", "Bush", "Clinton", "BushJr"};			   
   Object SelectedValue = JOptionPane.showInputDialog(null, "Choose a President", "List of Presidents to choose from:",JOptionPane.INFORMATION_MESSAGE, null,Presidents, Presidents[0]); 
   Cph main = new Cph();										 
   System.out.println(main.showPresidents(Presidents, Selected));							   
   again = JOptionPane.showConfirmDialog (null, "Would you like to try again? "); 		   
  }while (again == JOptionPane.YES_OPTION);
 }
 public static String showPresidents(Object[] Presidents, int Selected) 
 {
  StringBuilder PresChosen = new StringBuilder(); 								   
  if(Selected > 0)
  {
   PresChosen.append("\nPredecessor(Before): "); 								   
  }
  for(int i = 0; i < Selected; i++)
  {
   PresChosen.append(Presidents[i]);									  
  }
  if(Selected+1 < Presidents.length)
  {
   PresChosen.append("\nSuccesor(After): "); 									
  }
  for(int i = Selected+1; i < Presidents.length; i++)
  {
   PresChosen.append(Presidents[i]);									  
  }
  return PresChosen.toString(); 										
 }
}

As you can see, you set the selected to 0, retrieve a value from the user, and then do nothing with the new value, instead using only the 0-value.

you need to translate the selectedValue into the place in the array, or rewrite the showPresidents-method to use the selectedValue object instead. The most efficient way, in my opinion, would be to wrap the presidents in a class that knows its position.

Add this to your code:
Code:
class PW {// short for PresidentWrapper
String president;
int position;
public PW(String president, int position) {
this.president = president;
this.position = position;
}

// this is called by the JOptionPane, and decides what to show
public String toString() { return president; }

// we will use this to get the position
public int getPosition() { return position; }
}


and then change the main method as this:
Code:
 public static void main(String[] args)
 {
  int again, Selected = 0;
  do
  {
   JOptionPane.showMessageDialog(null, "-=Welcome=-");
   Object[] Presidents = {
new PW("Eisenhower",0),
new PW("Kennedy",1), 
new PW("Johnson",2), 
new PW("Nixon",3), 
new PW("Ford",4), 
new PW("Carter",5), 
new PW("Reagan",6), 
new PW("Bush",7), 
new PW("Clinton",8), 
new PW("BushJr",9)};			   
   Object SelectedValue = JOptionPane.showInputDialog(null, "Choose a President", "List of Presidents to choose from:",JOptionPane.INFORMATION_MESSAGE, null,Presidents, Presidents[0]); 
   Cph main = new Cph();

Selected = ((PW)SelectedValue).getPosition();
   System.out.println(main.showPresidents(Presidents,   Selected));							   
   again = JOptionPane.showConfirmDialog (null, "Would you like to try again? "); 		   
  }while (again == JOptionPane.YES_OPTION);
return; //ensures that the program exists :)
 }


And on a final note, it is custom for java applications to write all instances with first character lowercase. But some of your variables are uppercase. That make the code more difficult to understand. Consistency is a good thing .

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Jus need feedback and ideas


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap