In: Computer Science
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.
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///////////////////////////////////////////////