Question

In: Computer Science

1.Write the java code to create a two dimensional String array of sizes 12 by 8;...

1.Write the java code to create a two dimensional String array of sizes 12 by 8;

Given the java array:

      double[] test = new double[3];

2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell.

3. Can you have different types of data is a three dimensional array?

4. Given the java code:   int[][] values = new int[6][7]; How many integers can you put in this array?

5. Can you write over a file of data?

6. Write a java program that create a 2 dimensional array of type double of size 15 by 15. Fill the array with random numbers using a random number generator. Print the array contents.

7. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the even numbers 2 to 100 into the file. Close the file. Attach both files.

8. Create a java program that asks the user for the number of parts to be entered. Create an array of type String that is the size of the number the user provided. Ask the user for the part names, and place them in the array. Ask the user for the part name to find and search the array for the name. Tell the user if you found it or not.

9. Create an array with 100 random integers. Sort the array from low to high. Ask the user for a number to search. Search the array and let the user know if you found the number or not.

Solutions

Expert Solution

Done. Question 1-6 in file. ArrayManipulations.java

Question 7: FileHandling.java

Question 8,9: UserInputHandling.java

If you have doubt in any part please leave a comment I will revert back.

///////////////////////////////////////ArrayManipulations.java//////////////////////////////////

public class ArrayManipulations {

// 1. two dimensional String array of sizes 12 by 8

private String[][] twoDArr = new String[12][8];

static double[] test = new double[3];

public static void main(String args[]) {

// 2. put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in

// the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third

// cell

test[0] = 1.0;

test[1] = 2.0;

test[2] = 3.0;

// 3. Can you have different types of data is a three dimensional array?

// Ans: No you can not have data of different data types. if you want multiple

// types then better to create a custom object and use that as 3DArray. Eg you

// can create Student class with String, int , string fields in it.

// 4. How many integers can you put in this array

int[][] values = new int[6][7];

// there can be 6*7 interger in the array. 2D array is like a matrix where first

// arg is number of rows and number of coloums

// So in values array there can be 6 rows and each row can have 7 values which

// leads to 42

// 5. Can you write over a file of data

// Answer: Yes WHile working with file if we dont open the file in append mode

// data will be overwritten.

// FileOutputStream() constructor has a write() method. If the file

// does exist, any data inside it will be overwritten.

ques6();

}

// 6. Write a java program that create a 2 dimensional array of type double of

// size

// 15 by 15. Fill the array with random numbers using a random number generator.

// Print the array contents.

public static void ques6() {

// 6 Write a java program that create a 2 dimensional array of type double of

// size 15 by 15.

double arr6[][] = new double[15][15];

Random rand = new Random();

// Add random numbers to the array

for (int i = 0; i < arr6.length; i++) {

for (int j = 0; j < arr6[i].length; j++) {

int n = rand.nextInt(10) + 1;

arr6[i][j] = n;

}

}

// Print Array

for (int i = 0; i < arr6.length; i++) {

for (int j = 0; j < arr6[i].length; j++) {

System.out.print(arr6[i][j] + " ");

}

System.out.println();

}

}

}

///////////////////////////////////////End ArrayManipulations.java//////////////////////////////////

Output:

////////////////////////////////////FileHandling.java///////////////////////////////////////////////////////

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

// 7. Write a java program that creates a file called data.txt that uses the

// PrintWriter class. Write the even numbers 2 to 100 into the file. Close the

// file. Attach both files.

public class FileHandling {

private static String fileName = "data.txt";

public static void main(String args[]) {

FileWriter fileWriter;

try {

// Create fileWriter with the given file name

fileWriter = new FileWriter(fileName);

PrintWriter printWriter = new PrintWriter(fileWriter);

for (int i = 1; i <= 100; i++) {

// if the number is divisible by 2 then it is even. Add it to the files

if (i % 2 == 0) {

printWriter.println(i);

}

}

printWriter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

////////////////////////////////////End FileHandling.java///////////////////////////////////////////////////////

output in data.txt :

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

/////////////////////////////////////////UserInputHandling.java///////////////////////////////////////////////

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

//8. Create a java program that asks the user for the number of parts to be entered.

//Create an array of type String that is the size of the number the user provided.

//Ask the user for the part names, and place them in the array.

//Ask the user for the part name to find and search the array for the name.

//Tell the user if you found it or not.

public class UserInputHandling {

private static Scanner sc = new Scanner(System.in);

public static void main(String args[]) {

System.out.println("Please enter how any Number of parts you want to enter");

// get user input

int count = sc.nextInt();

// create Array that is the size of the number the user provided.

String[] parts = new String[count];

sc.nextLine();

// get user input for part name

for (int i = 0; i < count; i++) {

System.out.println("Please enter Part name:");

parts[i] = sc.nextLine();

}

System.out.println("Please enter Part name to search ");

String lookupPart = sc.nextLine();

// lookup for string entered by user in the array

boolean isPartValid = Arrays.stream(parts).anyMatch(lookupPart::equals);

System.out.println(lookupPart + " exists? " + isPartValid);

arrayManipulations();

}

// 9. Create an array with 100 random integers. Sort the array from low to high.

// Ask the user for a number to search. Search the array and let the user know

// if you found the number or not.

public static void arrayManipulations() {

// array with 100 size

int[] arr = new int[100];

Random rand = new Random();

for (int i = 0; i < arr.length; i++) {

// add random number to array

int n = rand.nextInt(100) + 1;

arr[i] = n;

}

// sort the array

Arrays.sort(arr);

System.out.println("Please enter the number to search");

int number = sc.nextInt();

// lookup the number in the array

boolean numExists = Arrays.binarySearch(arr, number) >= 0;

System.out.println(number + " exists in the array ? " + numExists);

}

}

/////////////////////////////////////////End UserInputHandling.java///////////////////////////////////////////////


Related Solutions

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT