Question

In: Computer Science

Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...

Java program

problem 1

  • Come up with an idea for parallel arrays
  • Create the arrays to hold 5 items in each
  • In the main(), load the arrays with data
  • Then output the entire contents of the arrays
  • Create a method called find1() and pass the 2 arrays to that method.

problem 2

  • In the find1() method, ask the user to enter a value to search for
  • Write logic to search the first array and then output the related data from the second array.
  • You can NOT use the .binarySearch() method. Create your own search logic.
  • The find1() method should return a TRUE if a match was found and FALSE if no match was found
    • Be sure to comment your code

Solutions

Expert Solution

Java code:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       String countries[] = {"India","US","UK","Srilanka","Bangladesh"};
       String capitals[] = {"Delhi","Washington","London","Colombo","Dhaka"};
       System.out.println("Printing Data in parallel arrays:");
       for(int i=0;i<5;i++)
       {
       System.out.println("Capital of "+countries[i]+" is "+capitals[i]);
       }
       System.out.println("-------------------------------------");
       find1(countries,capitals);
   }
   static boolean find1(String countries[],String capitals[])
{
String key;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Capital for search:");
key = sc.next();
for(int i=0;i<countries.length;i++)
{
if(key.equals(countries[i]))
{
System.out.println("key matched and its corresponding Capital is "+capitals[i]);
return true;
}

}
return false;
  
}
  
}

Execution Screenshots:


Related Solutions

how to create BANKACCOUNT program using Arrays in JAVA.
how to create BANKACCOUNT program using Arrays in JAVA.
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
Problem 1: In java Create a program to calculate interest rate and total balance - the...
Problem 1: In java Create a program to calculate interest rate and total balance - the program consists of (a) A class that encapsulates the interest rate and total balance calculation Constructor should accept initial balance and interest rate Methods: Returns interest rate Returns total balance Set interest rate Set initial balance (b) A test program that uses class from step (A) to perform the following: Set interest rate to 5% and initial balance to 1000 Print interest amount and...
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2)...
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2) a) How would you compare two arrays to check if they have the same values? b) Assume array1 and array2 are int arrays with 10 elements in each. if(array1 == array2) What is this comparing? 3 a) Can you encounter memory violation using an array? b) If yes explain. If no, explain why not.
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
C program: 1, Make two parallel arrays, one to keep track of food names and another...
C program: 1, Make two parallel arrays, one to keep track of food names and another to keep track of the food prices. There will be a total of 10 items, with a total of about 100 characters for each food name title (i think we can use malloc for the strings and allocate it accordingly) Basically it will be something like a menu: 1, we will enter the food information 2, it will list all the food names and...
(JAVA PLS)You will create a program that will follow the queuing theory of the Barbershop Problem....
(JAVA PLS)You will create a program that will follow the queuing theory of the Barbershop Problem. In this you will use ques and random number generator to complete this project. You will create three queues Couch - the area where patron will wait just prior to getting their hair cut; Line - the area where patron will form a line just before reaching the couch; and Cashier - the area where patrons will line up to pay just before exiting...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT