Question

Use java programming for the following questions to have the solutions ready within today Jan 3...


Use java programming for the following questions to have the solutions ready within today Jan 3 2020 at or before 8:00PM GMT+8 time zone:

Question 10

  1. Declare a local String variable id and initialize it with your 8-digit student ID in string form. What is the output of the following program segment?

String str = id + " "; // two spaces System.out.println(str.indexOf("5") + " + " + str.substring(0,2) + "\n"

+ str.trim().substring(5)+ "#");

  1. The class List below is in java.util. What is the output of the following program segment?

List<String> aList = new Vector<>();

aList.add("one");

aList.add("two");

aList.add("three");

aList.add(2, "one");

aList.remove("one");

System.out.println(aList + "\n" + aList.size());

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer

a)   Output

  It will output " index of the letter five + first two letters of the id " and on newline it prints "substring from 5 th position with a # symbol at the end"

working of each function

str.indexOf("5") - this will return the index of the first occurence of letter 5 in the id . If letter 5 is not present in id , then it will return -1

str.substring(0,2) - this will return the first two letters of the id, that is the letter at position 0 and 1

str.trim().substring(5) - trim function is used to remove the trailing and leading spaces in strings. so this will output a substring from the 5 th index of the string id which doesnt contains any leading spaces.

consider an example

if the variable   id="675439";

after executing this code
       String str = id + " "; // two spaces
       System.out.println(str.indexOf("5") + " + " + str.substring(0,2) + "\n"+ str.trim().substring(5)+ "#");

the output will be

2 + 67

9#

Here 2 is the index of letter 5 , 67 is the first two letters and 9 is the substring from 5 th index

b) Output

[two , one, three]

3

  

Reason :

aList.add("one"); -- add the string "one" to the list

after executing the below code

aList.add("one");

aList.add("two");aList.add("three");

the current list will be    [one , two, three]

aList.add(2, "one"); - this statement will add the string "one " to index 2 => 3 rd position

then the list will be  [one , two, one, three]

aList.remove("one"); - this will remove the element "one" from the string , here first occurence of one will be removed.

after that the list will be  

[two , one, three]

and its size is   3

if you find this answer useful ,please rate positive , thankyou

Add a comment
Know the answer?
Add Answer to:
Use java programming for the following questions to have the solutions ready within today Jan 3...
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
  • Use java programming for the following questions to have the solutions ready within today Jan 3...

    Use java programming for the following questions to have the solutions ready within today Jan 3 2020 at or before 8:00PM GMT+8 time zone: Question 5 A method secret() of a class F is shown below: public int secret(String[] stringArray, int n) { int num = 0; for (int i=0; i<stringArray.length; i++) if (stringArray[i].indexOf("a", n) >= 0) num++; return num; } A program segment using the method is: F f = new F(); String[] array01 = {"an", "easy", "exam", "on",...

  • Use java programming for the following questions to have the solutions ready within today Jan 3...

    Use java programming for the following questions to have the solutions ready within today Jan 3 2020 at or before 7:00PM GMT+8 time zone: Question 13 Some service fees (% of asset amount) of two investment companies are shown in the following tables: Company A: Service Fee Type Fee Management Fee 1.5 Subscription Fee 5.0 Redemption Fee 1.0 Company B: Service Fee Type Fee Platform Fee 0.2 Management Fee 1.0 Subscription Fee 2.0 The above information is to be stored...

  • DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code:...

    DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code: import java.util.Scanner; public class OddAndEven{ /* PART 1: Create a nonstatic method that takes in an int number quantity (n) and returns a returns a String of numbers from 0 to n (inclusive) as the example above demonstrates. Call this quantityToString.    In this method you should check that n is between 0(inclusive) and 100(inclusive). If n is outside these boundaries return and empty...

  • I have the following java-program, that creates a random walk starting at (0,0) and continuing until...

    I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...

  • Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use...

    Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. CODE PROVIDED: import java.util.ArrayList; import java.util.Scanner; public class Problem3 {    public static void main( String [] args ) {        Scanner in = new Scanner( System.in );        //PLEASE START YOUR WORK HERE        //**********************************************************                                                  //**********************************************************        // PLEASE END YOUR WORK HERE        System.out.print("END OF OUTPUT");    } } 20 points! PROBLEM 3: EXTENDED FAMILY Complete this...

  • in JAVA -- need to use an arraystack for this and can only manipulate smartstring.java aka...

    in JAVA -- need to use an arraystack for this and can only manipulate smartstring.java aka the one that says :               public class SmartString implements SmartStringADT at the beginning //for SmartStringTest.java: import static org.junit.Assert.*; import org.junit.Test; public class SmartStringTest {    //Test insert method    @Test    public void testinsert1() {        SmartString evaluator = new SmartString();        evaluator.insert(0, "Hello");        evaluator.insert(4, ", how are you?");        assertEquals("Hello, how are you?", evaluator.toString());    }   ...

  • Input hello, I need help completing my assignment for java,Use the following test data for input...

    Input hello, I need help completing my assignment for java,Use the following test data for input and fill in missing code, and please screenshot output. 1, John Adam, 3, 93, 91, 100, Letter 2, Raymond Woo, 3, 65, 68, 63, Letter 3, Rick Smith, 3, 50, 58, 53, Letter 4, Ray Bartlett, 3, 62, 64, 69, Letter 5, Mary Russell, 3, 93, 90, 98, Letter 6, Andy Wong, 3, 89,88,84, Letter 7, Jay Russell, 3, 71,73,78, Letter 8, Jimmie Wong,...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores....

    import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores. Repeats for more exams until the user says to stop. Input: Numbers, sum, answer Output: Displays program description Displays instruction for user Displays average Algorithm: 1. START 2. Declare variables sum, numberOf Students, nextNumber, answer 3. Display welcome message and program description 4. DOWHILE user wants to continue 5. Display instructions on how to use the program 6. Inititalize sum and number of student...

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