JAVA Can somone help with parts 4, 5, 6, 7 import java.util.Scanner; public class ArrayDemos { public static void ma

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

JAVA Can somone help with parts 4, 5, 6, 7 import java.util.Scanner; public class ArrayDemos { public static void ma

Post by answerhappygod »

JAVA Can somone help with parts 4, 5, 6, 7
import java.util.Scanner;
public class ArrayDemos
{
public static void main(String[] args)
{ System.out.print("\f");
//* ----------------- Problem 1
----------------------
System.out.println("\nP1");
// a) Declare an array of int called
count, that holds 5 int values
int [] count;
count = new int[4];
// b) Now assign the values 1, 5, 2, 9
and to the four elements of your array using index [0]
through [4]:
count[0] = 1;
count[1] = 5;
count[2] = 2;
count[3] = 9;

// c) Print count array using the while
loop illustrated at the bottom of section 12.1 of the
text.
int i = 0;
while (i < 4)
{
System.out.println(count + "
");
i++;
}
System.out.println();
// d) Change the values using some of
the techniques illustrated at the top in section 12.1.
count[0] = 7;
count[1] = count[0]*2;
count[2]++;
count[3]-=60;

// e) Print the count array a second
time to observe the changes made.
i = 0;
while (i < 4)
{
System.out.println(count + "
");
i++;
}


//
-----------------------------------*/

System.out.println("\nP2");
// a) Make a copy of your count array
called "copy" using the approach shown at top of sec12.2
//int [] copy = count;

// b) change the value of copy[0], then
print the count array a third time and observe the changes
//count[0] = 99;

// c) Make your copy array truly be an
independent copy using the approach shown at bottom of
sec12.2
int [] copy = new int[4];
i = 0;
while (i < 4)
{
copy = count;
i++;
}

copy[0] = 99;
System.out.println("Here's count");
i = 0;
while (i < 4)
{
System.out.println(count + "
");
i++;
}
System.out.println();

System.out.println("Here's copy");
i = 0;
while (i < 4)
{
System.out.println(count + "
");
i++;
}
System.out.println();
//
-----------------------------------*/


System.out.println("\nP3");

// Print the count array using a
for-loop as illustrated in section 12.4 and 12.5
for ( i = 0; i < count.length; i++) {
System.out.println(count);
}
System.out.println();




/* ------------problem 4
---------*/
System.out.println("\nP4");

// Generate an array "die100" with 100
throws of a die (from 1 to 6).
// how to generate the random numbers.
// 0) create an int array called die100 with
a size of 100 items
// 1) modify the for method with the following
formula (int) (x*6) + 1;
for ( i = 0; i < 10; i++){
double x = Math.random();
System.out.println(x);
}

// 2) once 1) works, Modiy the loop to
assign the value being printed
// to index i in the die100 array (make
the loop go up to 100)
// also, define a printArray method
below main to print your int array.
System.out.println("die100 contains the
values: ");
// printArray(die100);

// also, define a printArray method
below main to print your int array.
System.out.println("die100 contains the
values: ");
// printArray(die100);

//
-----------------------------------*/
/* ----------------- Problem 5
----------------------
System.out.println("\nP5");
// Define a method count5(int [] arr)
below main that returns the count of the number of
// times a 5 is found in the
array.

System.out.println("The number 5
appeared " + count5(die100) + " in the die100 array");



//
-----------------------------------*/
//* ----------------- Problem 6
----------------------
System.out.println("\nP6");
// Define an int array counts that has
7 elements in it.
// Follow the insructions in section
12.10 to create a histogram of the values in die100 stored
// in the array counts. Then print your
counts array from 1 to 6 and check if it represents the
// true count of values in die100 (at
least you can check if the count of 5's is the same as
// is count5 produces - there should be
around 16 5's more or less).



System.out.println("The histogram of
die100 is ");
// printArray(counts); // in
viewing the results, are the numbers evenly distributed?


// EXTRA CREDIT: display your results
as a bar chart like the one shown in slide 42 (and 43)


//
-----------------------------------*/
/* ----------------- Problem 7
----------------------
System.out.println("\nP7");
// Generate an array called "arr10"
with 10 random values between 0 and 99

System.out.println("arr10 contains the
values: ");
printArray(arr10);

// Define a method maxValue that
returns the largest number in an array
System.out.println("the largest value
in arr10 is " + maxValue(arr10);


//
-----------------------------------*/
/* ----------------- Problem 8 EXTRA
CREDIT ----------------------
System.out.println("\nP8");
// Develop a bubbleSort method
following the example slides 44-63 and use it
// to sort your arr10 array from
problem 6

bubbleSort(arr10);
System.out.println("after sorting
arr10, we have: ");
printArray(arr10);


//
-----------------------------------*/

} // end of main
public static int count5(int [] arr)
{
int counter = 0;
for (int i=0; i < arr.length;
i++)
{

}
return counter;
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply