Question

Hello, we are currently learning the compareTo() method in my Java Data Structures class. I am...

Hello, we are currently learning the compareTo() method in my Java Data Structures class. I am having trouble understanding what the output here will be:

a = "ballD";

b= "ballDs";

System.out.println(a.compareTo(b));

I don't know how to interpret the small "s". I know that if it was a = "ballD"; b = "ballD"; the output would be 0.

Please help, thanks in advance

0 0
Add a comment Improve this question Transcribed image text
Answer #1
If the first string is prefix of the second string then compareTo() returns negative of number of extra characters in string2.
If the second string is prefix of the first string then compareTo() returns number of extra characters in string2.

Example 1:
----------
a = "ballD";
b= "ballDs";
System.out.println(a.compareTo(b));
This prints -1
System.out.println(b.compareTo(a));
This prints 1

Example 2:
----------
a = "ballD";
b= "ballDst";
System.out.println(a.compareTo(b));
This prints -2
System.out.println(b.compareTo(a));
This prints 2

Example 3:
----------
a = "ballD";
b= "ballDstu";
System.out.println(a.compareTo(b));
This prints -3
System.out.println(b.compareTo(a));
This prints 3

Add a comment
Know the answer?
Add Answer to:
Hello, we are currently learning the compareTo() method in my Java Data Structures class. I am...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I am a student studying abroad, currently i have to work on some assignments my...

    Hello, I am a student studying abroad, currently i have to work on some assignments my professor gave me to solve. Since my professor doesnt have the best grasp on the english language i am having trouble understanding what he requires me to do. I know its the holidays, but i could really use some help here ! Happy New Year to you all Imagine a loan of 20.000 ₤ with an interest rate of 7% a year. Given that...

  • Hello, I am a student studying abroad, currently i have to work on some assignments my...

    Hello, I am a student studying abroad, currently i have to work on some assignments my professor gave me to solve. Since my professor doesnt have the best grasp on the english language i am having trouble understanding what he requires me to do. I know its the holidays, but i could really use some help here ! Happy New Year to you all Someone decided to deposit 100 € in a bank that pays an effective interest rate of...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Hey, I have an assignment from my Data Structures class. I need the output to be:...

    Hey, I have an assignment from my Data Structures class. I need the output to be: [1] to set the student name [2] to set the student address [3] to set the student GPA [4] to set the student student ID [5] to set the student major [6] to print student info [7] to exit Then I need this output to repeat another 2 times. How can I code that? Its java Also, to code the output above, I used...

  • hello i am doing a research paper for my economic class about that question if you...

    hello i am doing a research paper for my economic class about that question if you guys can provide with some good paragraphs preferable own words and no internet I would really appreciate it thanks in advance. in what ways income inequality affects poverty?

  • Hello, I am in Physics 102 in college and am currently in a unit on circuits....

    Hello, I am in Physics 102 in college and am currently in a unit on circuits. We are studying voltage, current, resistance, capacitance, etc. The majority of the problems we work on are out of the "Fundamentals of Physics, 9th edition" textbook. I have been using your answers to guide me in my homework, but I would eventually like to be able to have a strategy to conquer these problems on my own. Most of the time I cant figure...

  • easy question but i am confused. someone help Consider the following Java classes: class OuterClass {...

    easy question but i am confused. someone help Consider the following Java classes: class OuterClass { static class InnerClass { public void inner Method({ System.out.println("This is my inner class"); public static void outer Method{ System.out.println("This is my outer class"); public class OuterClass Test{ public static void main(String args[]) { Complete the class OuterClass Test to produce as output the two strings in class OuterClass. What are the outputs of your test class?

  • Hi so I am currently working on a project for a java class and I am...

    Hi so I am currently working on a project for a java class and I am having trouble with a unit testing portion of the lab. This portion wants us to add three additional tests for three valid arguments and one test for an invalid argument. I tried coding it by myself but I am not well versed in unit testing so I am not sure if it is correct or if the invalid unit test will provide the desired...

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • Hello, I am having trouble making sense into what to do here, and I don't know...

    Hello, I am having trouble making sense into what to do here, and I don't know how to answer them. any help in answering them is appreciated! thank you lastName E101 E115 P10 P15 P10 E115 proj Mane Columbia how the final tables that would be produced by each of the following relational algebra Il sert(IIrrasse (Assign) Proj)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT