|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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!
|
|
#2
|
|||
|
|||
|
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) {
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 02:40 PM. |
|
#3
|
|||
|
|||
|
// 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? |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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();
}
}
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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 . |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Jus need feedback and ideas |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|