Re: Operations with lists
Please post what you have so far along with a more detailed question. Simply posting the homework assignment with no indication of where you are, what your questions are, the effort made to solve this problem, etc...does not provide any information for anyone to help aside from posting links to general tutorials, such as these:
How To Ask Questions The Smart Way
The Java™ Tutorials
Re: Operations with lists
Hello and thank you for your answer.
That's just it: I don't know where to start. To be sure, I'm not looking for someone to put here the code, I just wanted someone to point me in the right direction.
So far, I have only one class: the menu one with only that, the menu. But I can post it here if that's what you want:
PHP Code:
import javax.swing.JOptionPane;
public class menu {
public static int options() {
int op;
do {
String a = "Menu"
+ "\n1 - write a sentence"
+ "\n2 - words in a reverse order"
+ "\n3 - create an ordered bidirectional list with words that are not repeated"
+ "\n4 - delete word"
+ "\n5 - delete word between x and y"
+ "\n6 - Move nodes"
+ "\n7 - Copy nodes"
+ "\n8 - print lists"
+ "\n0 - exit";
op = Integer.parseInt(JOptionPane.showInputDialog(a + "\nOption? (0 to 8)"));
}
while ((op < 0) || (op > 8));
return op;
}
That's it. What do I need to do next? Create a class for the 3 lists or a separate for each?
Once again, thank you for your answer.
Regards.
Re: Operations with lists
Sorry, but where in your requirements does it say to create a GUI? Are you sure you're not just supposed to do all of this on the command line?
Re: Operations with lists
Actually, it doesn't. I just assumed it would be easier to do it like that. Am I wrong?
Re: Operations with lists
Ok, after some documentation checking, I've come up with this. Can someone take a look if I'm on the right track? How can I see if both lists are ok?
PHP Code:
import javax.swing.JOptionPane;
public class menu {
public static int options() {
int op;
do {
String a = "Menu"
+ "\n1 - write a sentence"
+ "\n2 - words in a reverse order"
+ "\n3 - create an ordered bidirectional list with words that are not repeated"
+ "\n4 - delete word"
+ "\n5 - delete word between x and y"
+ "\n6 - Move nodes"
+ "\n7 - Copy nodes"
+ "\n8 - print lists"
+ "\n0 - exit";
op = Integer.parseInt(JOptionPane.showInputDialog(a + "\nOption? (0 to 8)"));
}
while ((op < 0) || (op > 8));
return op;
}
public static void main(String[] args) {
list A = new list();
list B = new list();
int option = opcoes();
while (option != 0) {
String sentence;
switch (option) {
case 1: {
do
sentence = JOptionPane.showInputDialog("Input sentence: ");
while (sentence.compareTo("") == 0);
A.receivesentence(sentence);
break;
}
case 2: {
do
sentence = JOptionPane.showInputDialog("Input sentence: ");
while (sentence.compareTo("") == 0);
B.receivesentence2(sentence);
break;
}
case 3: {
break;
}
}
}
}
}
PHP Code:
import java.util.*;
public class list {
private node head;
public void receivesentence(String sentence){
String [] words = sentence.split(" ");
for (int i = 0; i < words.length-1; i++){
this.insertordered(words[i]);
}
}
public void receivesentence2(String sentence){
String [] words = sentence.split(" ");
for (int i = 0; i < words.length-1; i++){
this.insertinverted(words[i]);
}
}
public void insertordered(String word){
node new=new node(word);
node temp=head;
while((temp.getnext()!=null)&&(temp.getnext().getword().compareTo(new.getword())<0)){
temp=temp.getnext();
}
new.setnext(temp.getnext());
new.setprev(temp);
temp.setnext(new);
}
public void insertinverted(String word){
node new=new node(word);
node temp=head;
while((temp.getnext()!=null)&&(temp.getnext().getword().compareTo(new.getword())>0)){
temp=temp.getnext();
}
new.setnext(temp.getnext());
new.setprev(temp);
temp.setnext(new);
}
public String print()
{
String st = "";
if (head.getnext() != head)
{
node temp = head.getnext();
while(temp != head)
{
st += temp.getword() + " ";
temp = temp.getnext();
}
}
else
st += "The list is empty!";
return st;
}
}
PHP Code:
public class node {
private String word;
private node next;
private node prev;
public node(String word){
this.word=word;
}
public String getword() {
return word;
}
public void setword(String word) {
this.word = word;
}
public node getnext() {
return next;
}
public void setnext(node next) {
this.next = next;
}
public node getprev() {
return prev;
}
public void setprev(node prev) {
this.prev = prev;
}
}
Re: Operations with lists
Good morning.
Can someone help me with this one? It's due today...
Re: Operations with lists
If your requirements don't say to create a GUI, I would think that a GUI is overkill. Not sure whether you'd get points off or not.
Anyway, I'd be happy to help if you posted an SSCCE and described what's happening vs what you think should be happening.
Re: Operations with lists
It doesn't say anywhere but it's pretty much common practice. Ok, thank you for your answer.
So, now it's placing the sentence inside the A list but not on the B list. And, for some reason, it's not placing on the correct order on both of them.
I have the list class like this, now:
PHP Code:
package pacote;
public class list {
private node cab;
public list()
{
cab = new node("");
cab.setprev(cab);
cab.setnext(cab);
}
public void receiveinsert(String insert){
String [] words = insert.split(" ");
for (int i = 0; i < words.length; i++){
this.insertordered(words[i]);
}
}
public void receiveinsert2(String insert){
String [] words = insert.split(" ");
for (int i = 0; i < words.length; i++){
this.insertinverted(words[i]);
}
}
public void insertordered(String word){
node newnode=new node(word);
node temp=cab.getnext();
while(temp.getnext()!=cab &&(temp.getnext().getword().compareTo(newnode.getword())<0)){
temp=temp.getnext();
}
newnode.setnext(temp.getnext());
newnode.setprev(temp);
temp.setnext(newnode);
}
public void insertinverted(String word){
node newnode=new node(word);
node temp=cab.getnext();
while(temp.getnext()!=cab &&(temp.getnext().getword().compareTo(newnode.getword())>0)){
temp=temp.getnext();
}
newnode.setnext(temp.getnext());
newnode.setprev(temp);
temp.setnext(newnode);
}
public String printordered()
{
String st = "";
node temp = cab.getnext();
if (temp!= cab)
{
while(temp != cab)
{
st += temp.getword() + " ";
temp = temp.getnext();
}
}
else
st += "The list is empty!";
return st;
}
public String printinverted()
{
String st = "";
node temp = cab.getprev();
if (temp!= cab)
{
while(temp != cab)
{
st += temp.getword() + " ";
temp = temp.getprev();
}
}
else
st += "The list is empty!";
return st;
}
}
And I also had to change the menu class:
PHP Code:
public static void main(String[] args) {
list A = new list();
list B = new list();
int option = options();
while (option != 0) {
String sentence;
switch (option) {
case 1:
{
sentence = JOptionPane.showInputDialog("Input sentence: ");
A.receivesentence(sentence);
B.receivesentence2(sentence);
break;
}
case 2:
{
// sentence = JOptionPane.showInputDialog("Input sentence to reverse: ");
// B.receivesentence2(sentence);
break;
}
case 3: {
break;
}
case 8:
{
JOptionPane.showMessageDialog(null,A.printordered());
JOptionPane.showMessageDialog(null,B.printinverted());
break;
}
}
option = options();
}
}
}