im coding a program to put names lexicographically. this is my code: public clas
ID: 3544456 • Letter: I
Question
im coding a program to put names lexicographically. this is my code:
public class Lex1 {
String s1, s2, s3, first, second, third;
public Lex1 (String string1, String string2, String string3)
{
s1 = string1;
s2 = string2;
s3 = string3;
}
public String getString1()
{
if (s1.compareTo(s2) < 0 && s1.compareTo(s3) < 0)
first = s1;
if (s2.compareTo(s1) < 0 && s2.compareTo(s3) < 0)
first = s2;
if (s3.compareTo(s1) < 0 && s3.compareTo(s2) < 0)
first = s3;
return first;
}
public String getString2()
{
if (s1.compareTo(s2) < 0 && s1.compareTo(s3) > 0)
second = s1;
if (s2.compareTo(s1) < 0 && s2.compareTo(s3) > 0)
second = s2;
if (s3.compareTo(s1) < 0 && s3.compareTo(s2) > 0)
second = s3;
return second;
}
public String getString3()
{
if (s1.compareTo(s2) > 0 && s1.compareTo(s3) > 0)
third = s1;
if (s2.compareTo(s1) > 0 && s2.compareTo(s3) > 0)
third = s2;
if (s3.compareTo(s1) > 0 && s3.compareTo(s2) > 0)
third = s3;
return third;
}
}
Runner:
public class Lexicographical {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter three names");
String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();
Lex1 test = new Lex1(s1, s2, s3);
System.out.println(" ");
System.out.println(test.getString1());
System.out.println(test.getString2());
System.out.println(test.getString3());
}
}
Explanation / Answer
import java.util.Scanner;
class Lex1 {
String s1, s2, s3, first, second, third;
public Lex1(String string1, String string2, String string3) {
s1 = string1;
s2 = string2;
s3 = string3;
}
public String getString1() {
if (s1.compareTo(s2) < 0 && s1.compareTo(s3) < 0) {
first = s1;
}
if (s2.compareTo(s1) < 0 && s2.compareTo(s3) < 0) {
first = s2;
}
if (s3.compareTo(s1) < 0 && s3.compareTo(s2) < 0) {
first = s3;
}
return first;
}
public String getString2() {
if (s1.compareTo(s2) < 0 && s1.compareTo(s3) > 0) {
second = s1;
}
if (s2.compareTo(s1) < 0 && s2.compareTo(s3) > 0) {
second = s2;
}
if (s3.compareTo(s1) < 0 && s3.compareTo(s2) > 0) {
second = s3;
}
return second;
}
public String getString3() {
if (s1.compareTo(s2) > 0 && s1.compareTo(s3) > 0) {
third = s1;
}
if (s2.compareTo(s1) > 0 && s2.compareTo(s3) > 0) {
third = s2;
}
if (s3.compareTo(s1) > 0 && s3.compareTo(s2) > 0) {
third = s3;
}
return third;
}
}
class Lexicographical {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter three names");
String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();
Lex1 test = new Lex1(s1, s2, s3);
System.out.println(" ");
System.out.println(test.getString1());
System.out.println(test.getString2());
System.out.println(test.getString3());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.