Access Millions of academic & study documents

Along with being a magician, you have also taught yourself the art o

Content type
User Generated
Showing Page:
1/5
Along with being a magician, you have also taught yourself
the art of trickery by watching open courseware videos on
Trickery 101. You have achieved mastery in converting I
owe yous into You owe mes In the program below, you
are given an array of integers called iou. You have to
write code which will negate every number in the array
What we expect: If the array iou contains [2,4,6], you
must change it to contain [-2,-4,-6]. You do not need to
create a new array to store the result. Replace numbers in-
place in the array \'iou\' itself, with their negated values.
You do not need to print anything in this exercise.
Learning outcomes: After completing this exercise you
should have learned how to iterate across an array, and
how to read as well as write values into the array.
------------------------------------
import java.util.Scanner;
public class Trickster {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println(\"Enter a few positive integers,
separated with spaces, which represent the IOU\'s you owe
to friends: \");
String lineIous = scanner.nextLine();
int iou[] = getIntArrFromArgs(lineIous);
///{
//write your code hare
//start
//
///}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/5
print(iou);
}
private static void print(int[] iou) {
StringBuffer buff = new StringBuffer(\"\");
for(int i=0; i<iou.length; i++) {
buff.append(iou[i] + \" \");
}
System.out.println(buff.toString().trim());
}
private static int[] getIntArrFromArgs(String lineIous) {
String args[] = lineIous.split(\"\\\\s+\");
int iqs[] = new int[args.length];
for(int i=0; i<args.length; i++) {
iqs[i] = Integer.parseInt(args[i]);
if(iqs[i] < 0) {
String msg = \"You must enter positive integers.
\" + args[i] + \" is not a positive integer\";
throw new IllegalArgumentException(msg);
}
}
return iqs;
}
}
Solution
import java.util.Scanner;
public class Trickster {

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/5

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 5 pages?
Access Now
Unformatted Attachment Preview
Along with being a magician, you have also taught yourself the art of trickery by watching open courseware videos on Trickery 101. You have achieved mastery in converting “I owe you’s” into “You owe me’s” In the program below, you are given an array of integers called ‘iou’. You have to write code which will negate every number in the array What we expect: If the array ‘iou’ contains [2,4,6], you must change it to contain [-2,-4,-6]. You do not need to create a new array to store the result. Replace nu mbers inplace in the array \'iou\' itself, with their negated values. You do not need to print anything in this exercise. Learning outcomes: After completing this exercise you should have learned how to iterate across an array, and how to read as well as write values into the array. -----------------------------------import java.util.Scanner; public class Trickster { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println(\"Enter a few positive integers, separated with spaces, which represent the IOU \'s you owe to friends: \"); String lineIous = scanner.nextLine(); int iou[] = getIntArrFromArgs(lineIous); ///{ //write your code hare //start // ///} print(iou); } private static void print(int[] iou) { StringBuffer buff = new StringBuffer( \"\"); for(int i=0; i ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4