Avatar billede salimbharuchi Nybegynder
25. september 2009 - 09:46 Der er 11 kommentarer og
1 løsning

String Sorter

Hi Experts,
I am new to java programming, and i've an assignment from college, please help me with this question:

Write a program that reads in three strings and prints the lexicographically smallest and
largest one:
Please enter three strings:
Tom
Dick
Harry
The inputs in sorted order are:
Dick
Harry
Tom
Use the following class as your main class:
import java. util.Scanner;
/**
This is program sorts three strings.
*/
public class StringSorter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter three strings:");
String str1 = in.nextLine();
String str2 = in.nextLine();
String str3 = in.nextLine();
StringSet s = new StringSet(str1, str2, str3);
System.out.println("The inputs in sorted order are:\n"
+ s.getSmallest() + "\n"
+ s.getMiddle() + "\n"
+ s.getLargest());
}
}
Complete the following class in your solution:
/**
This class finds the smallest, middle, and largest of
three strings.
*/
public class StringSet
{
/**
Constructs a string set that processes three strings.
@param str1 the first string to sort
@param str2 the second string to sort
@param str3 the third string to sort
*/
public StringSet(String str1, String str2, String str3)
{
. . .
}
/**
Gets the smallest string in the string set.
@return smallest the smallest of three strings
*/
public String getSmallest()
{
. . .
}
/**
Gets the largest string in the string set.
@return largest the largest of three strings
*/
public String getLargest()
{
. . .
}
/**
Gets the middle string in the string set.
@return middle the middle string of three strings
*/
public String getMiddle()
{
. . .
}
. . .
}

Help would be appreciated..
Avatar billede arne_v Ekspert
25. september 2009 - 17:01 #1
You need 3 fields that the getters can return.

And then you need the constructor to set those 3 fields.

The simplest solution is probably just to use if statements that compare the strings.
Avatar billede salimbharuchi Nybegynder
25. september 2009 - 18:13 #2
Can you please send me the solution...using if statements..
Avatar billede arne_v Ekspert
25. september 2009 - 19:53 #3
You will not learn much by me giving you the solution.

You can approach it either with 6 if's each covering a case or by nesting if's.

I think the first would be the easiest.

Give it a try.
Avatar billede salimbharuchi Nybegynder
26. september 2009 - 13:44 #4
public class StringSet
    {
   
    public StringSet(String    string1, String string2, String string3)
    {
//        if(str1.compareTo(str2)<0 && str1.compareTo(str3)<0)
//            Minimum = str1;
//        else if(str1.compareTo(str2)>0 && str1.compareTo(str3)>0)
//            Maximum = str1;   
//        else if(str2.compareTo(str1)<0 && str2.compareTo(str3)<0)
//            Minimum = str2;
//        else if(str2.compareTo(str1)>0 && str2.compareTo(str3)>0)
//            Maximum = str2;   
//        else if(str3.compareTo(str1)<0 && str3.compareTo(str2)<0)
//            Minimum = str3;
//        else if(str3.compareTo(str1)>0 && str3.compareTo(str2)>0)
//            Maximum = str3;   
//        else
//            Maximum = "Number not found";   
               
        // Compare string1 with string3
        if(string1.compareTo(string3) < 0)
        {
        Minimum = string1;
        }
        else
        {
        if(string1.compareTo(string3) > 0)
        {
        Maximum = string1;
        }
        else
        {
        Middle = string1;
        }
        }
        if(string2.compareTo(string1) < 0)
        {
        Minimum = string2;
        }
        else
        {
        if(string2.compareTo(string1) > 0)
        {
        Maximum = string2;
        }
        else
        {
        Middle = string2;
        }
        }
        if(string2.compareTo(string3) < 0)
        {
        Minimum = string2;
        }
        else
        {
        if(string2.compareTo(string3) > 0)
        {
        Maximum = string2;
        }
        else
        {
        Middle = string3;
        }
        }               
           
           
    }
   
    public String getSmallest()
    {
        return Minimum;
    }
   
   
    public String getLargest()
    {
        return Maximum;
    }
   
   
   
   
    private String Minimum;
    private String Maximum;
    private String Middle;
    }
This is what i have so far...
Avatar billede arne_v Ekspert
26. september 2009 - 22:15 #5
if(str1.compareTo(str2) < 0 && str1.compareTo(str3) < 0 && str2.compareTo(str3) < 0) {
    minimum = str1;
    middle = str2;
    maximum = str3;
} else if(
Avatar billede salimbharuchi Nybegynder
27. september 2009 - 01:45 #6
Does'nt work...output is null for all values...
Avatar billede arne_v Ekspert
27. september 2009 - 04:44 #7
Can you post the complete code that gives null for all values?
Avatar billede salimbharuchi Nybegynder
27. september 2009 - 14:46 #8
public class StringSet
    {
   
    public StringSet(String    string1, String string2, String string3)
    {
               
    if(string1.compareTo(string2) < 0 && string1.compareTo(string3) < 0 && string2.compareTo(string3) < 0)
        {
            Minimum = string1;
            Middle = string2;
            Maximum = string3;
        }        


   
    public String getSmallest()
    {
        return Minimum;
    }
   
   
    public String getLargest()
    {
        return Maximum;
    }
   
   
       
    public String getMiddle()
    {
        return Middle;
    }
   
    private String Minimum;
    private String Maximum;
    private String Middle;
    }

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

import java. util.Scanner;
/**
This is program sorts three strings.
*/
public class StringSort
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Please enter three strings:");
String str1 = in.nextLine();
String str2 = in.nextLine();
String str3 = in.nextLine();
StringSet s = new StringSet(str1, str2, str3);
System.out.println("The inputs in sorted order are:\n"
+ s.getSmallest() + "\n"
+ s.getMiddle() + "\n"
+ s.getLargest());   
}
}
Avatar billede arne_v Ekspert
27. september 2009 - 23:33 #9
The code works fine for the particular case described in the if statement.

So you just need 5 more if statements!
Avatar billede salimbharuchi Nybegynder
10. oktober 2009 - 21:01 #10
Thanks a lot it works fine...
Avatar billede arne_v Ekspert
10. oktober 2009 - 22:03 #11
Then I will drop a real answer.
Avatar billede salimbharuchi Nybegynder
10. oktober 2009 - 22:10 #12
Yes..No prbs at all..
I've one more question shall i post it here or in a new question..
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester