★ preLabC.java 1- import java.util.Random; 2 import java.util.StringJoiner; 3 4- public class preLabC { 5 6- 7 8 9 10 11 12 13 14 15 16 17 18 19 20 } public static String myMethod(MathVector inputVector) { String vectorStringValue = new String(); // empty String object System.out.println("here is the contents object, inputVector: " + inputVector); // get a String out of that MathVector object. Do it here. On the next line. } // return the double value here. return vectorStringValue; ▸ Compilation ▾ Description A MathVector object will be passed to your method. Return its contents as a String. If you look in the file MathVector.java you'll see there is a way to output the contents of a MathVector object as a String. This makes it useful for displaying to the user. Returns a String representation of this vector. The String should be in the format "[1, 2, 3]" * @return a String representation of this vector QapiNote DO NOT use the built-in (@code Arrays.toString()) method. @Override public String toString() { var sj= new StringJoiner(",", "[", "1"); for (var e: array) { sj.add(String.valueOf(e)); } return sj.toString(); You might have noticed that there's an @override term there. That's because many objects already have a "toString()" method associated with them... because Java was designed to include them by default. Here, the override tells Java "I know, I know. You already have a toString() that you'd assign here. But it's not good enough. Here's a better one for this particular kind of object." It's a little bit like saying "Most humans have two legs. So, by default, I'll give everyone two legs. But sometimes we override that and give no legs, or just one leg to a person. And sometimes we give them four so that they can be a centaur!" To use this in a println() method, just name your object. The toString() method will be called implicitly. Or you can just write System.out.println("look!" + myObject.toString()); C

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 12RQ
icon
Related questions
Question
preLabC.java U
1 import java.util. Random;
2
3
4 public class preLabC {
5
6 ▼
7
8
9
10
11
12
13
import java.util.StringJoiner;
14
15
16
17
18
19
20 }
public static String myMethod(MathVector inputVector) {
String vectorStringValue = new String(); // empty String object
System.out.println("here is the contents object, inputVector: + inputVector);
// get a String out of that MathVector object. Do it here.
}
// return the double value here.
return vectorStringValue;
On the next line.
▸ Compilation
▾ Description
A MathVector object will be passed to your method. Return its contents as a String.
If you look in the file MathVector.java you'll see there is a way to output the contents of a
MathVector object as a String. This makes it useful for displaying to the user.
/***
* Returns a String representation of this vector. The String should be in the format "[1, 2, 3]"
*
* @return a String representation of this vector
* @apiNote **DO NOT** use the built-in (@code Arrays.toString()} method.
*/
@Override
public String toString() {
var sj= new StringJoiner(",", "[", "]");
for (var e: array) {
}
}
sj.add(String.value0f (e));
return sj.toString();
You might have noticed that there's an @override term there. That's because many objects
already have a "toString()" method associated with them... because Java was designed to
include them by default. Here, the override tells Java "I know, I know. You already have a
toString() that you'd assign here. But it's not good enough. Here's a better one for this
particular kind of object." It's a little bit like saying "Most humans have two legs. So, by
default, I'll give everyone two legs. But sometimes we override that and give no legs, or
just one leg to a person. And sometimes we give them four so that they can be a centaur!"
To use this in a println() method, just name your object. The toString()
method will be called implicitly. Or you can just write
System.out.println("look! + myObject.toString());
Transcribed Image Text:preLabC.java U 1 import java.util. Random; 2 3 4 public class preLabC { 5 6 ▼ 7 8 9 10 11 12 13 import java.util.StringJoiner; 14 15 16 17 18 19 20 } public static String myMethod(MathVector inputVector) { String vectorStringValue = new String(); // empty String object System.out.println("here is the contents object, inputVector: + inputVector); // get a String out of that MathVector object. Do it here. } // return the double value here. return vectorStringValue; On the next line. ▸ Compilation ▾ Description A MathVector object will be passed to your method. Return its contents as a String. If you look in the file MathVector.java you'll see there is a way to output the contents of a MathVector object as a String. This makes it useful for displaying to the user. /*** * Returns a String representation of this vector. The String should be in the format "[1, 2, 3]" * * @return a String representation of this vector * @apiNote **DO NOT** use the built-in (@code Arrays.toString()} method. */ @Override public String toString() { var sj= new StringJoiner(",", "[", "]"); for (var e: array) { } } sj.add(String.value0f (e)); return sj.toString(); You might have noticed that there's an @override term there. That's because many objects already have a "toString()" method associated with them... because Java was designed to include them by default. Here, the override tells Java "I know, I know. You already have a toString() that you'd assign here. But it's not good enough. Here's a better one for this particular kind of object." It's a little bit like saying "Most humans have two legs. So, by default, I'll give everyone two legs. But sometimes we override that and give no legs, or just one leg to a person. And sometimes we give them four so that they can be a centaur!" To use this in a println() method, just name your object. The toString() method will be called implicitly. Or you can just write System.out.println("look! + myObject.toString());
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Methods of StringBuilder class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,