Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help explain these? The Java solutions are given too. Trying to study and learn the logic. Questions 1. Write code using for loops that

Please help explain these? The Java solutions are given too. Trying to study and learn the logic.

Questions

1. Write code using for loops that will display one rightside-up triangle with a base width of 5, that looks identical to the example below:

 * ** *** **** ***** 

You will receive no credit if you solve this without proper use of nested for loops.You may not use any individual print (or println, printf, etc.) statements that include more than one asterisk. (There are no such restrictions regarding other string characters.)

You do not need to provide infrastructure such as a class statement, a main method statement, etc.

Solution: The key is to realize you are drawing two triangles, one of asterisks and the other of blank spaces. Heres my solution:

 for (int i = 1; i <= 5; i++) { for (int ispaces = 0; ispaces < 5-i; ispaces++) { 
 System.out.print(" "); 

As of May 2, 2017. Acknowledgment: Some/all portions are copied from previous UWB CSS exams.1

}

 for (int istar = 0; istar < i; istar++) { System.out.print("*"); 

}

 System.out.print(" "); } 

2. Lets say you have a text file called data.txt that contains a single column of integers. Write the Java code (presume its in a main method) that will read in this file and print to console double each number you read in. Thus, if the file has the lines:

2 4 -5 11

your program will write the following to the screen:

4 8 -10 22

Make sure your program includes all necessary file opening and closing commands. Your program should also correct catch any file opening errors. You can assume all necessary classes have already been imported in. Your program should work withdata.txt of any size.

Solution:

 Scanner inputStream = null; try { 
 inputStream = new Scanner( new FileInputStream("data.txt")); } 
 catch(FileNotFoundException e) { 
 System.out.println("Problem opening files."); 
 System.exit(0); } 
 int number; while (inputStream.hasNextInt( )) { 

2

 number = inputStream.nextInt(); number = 2 * number; System.out.println(number); 

}

 inputStream.close(); } 

Write a method that takes two int arrays as input and returns an int array where each element is the product of the corresponding elements of the two input arrays. Youcannot assume that the two arrays are the same length; the method has to check for that condition. If the two arrays are not the same length, the return value should be a single element int array with the value of 1.

Solution:

 public int[] multiplyArrays(int[] array1, int[] array2) { 
 if (array1.length != array2.length) { int[] output = new int[1]; output[0] = -1; return output; 
 } else { int[] output = new int[array1.length]; for (int i = 0; i < array1.length; i++) { 
 output[i] = array1[i] * array2[i]; } 
 return output; } 

} Remember, the output array has to be declared in a place where the return

statement can see it; be careful of scope.

Write code that creates a 3 row, 7 column int array whose values in each element are the product of the row and column indices for that array. That is, the contents of the array should be:

0000000 0123456 0 2 4 6 8 10 12

You may not use the curly-brace syntax to manually type out and fill the above contents of the array.

After you create the array, write the code to change all the elements of the array so they are doubled three times. That is, after the three-fold doubling, the array will have the values:

3

0000000 0 81624324048 0 16 32 48 64 80 96

You may not use the exponentiation operator nor more than a single multiplication symbol in this second portion. That is to say, you have to do this three-fold doubling via a loop.

You do not need to provide infrastructure such as a class statement, a main method statement, etc.

Solution:

 int[][] a = new int[3][7]; 
 for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { 
 a[i][j] = i * j; } 

}

 for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { 
 for (int k = 0; k < 3; k++) { a[i][j] *= 2; 

} }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 12c SQL

Authors: Joan Casteel

3rd edition

1305251032, 978-1305251038

More Books

Students also viewed these Databases questions

Question

4-19. When you say weve doubled our profit level, you are (wrong).

Answered: 1 week ago