Question

Given the following problem, explain what instance variables and methods you would create, what parameters would...

Given the following problem, explain what instance variables and methods you would create, what parameters would be sent to each method (if any) and what values would be returned (if any), and an overview of what the method will do. You should explain why you have chosen to break the problem down as you have.

You are required to write a program in JAVA which will fill an array with names entered by the user. You must be able to output the list on one line in proper order, and also in reversed order on one line. As well, you should be able to search for a name in the list (entered by the user in the main method) and send the position number of the value back to the main method for future use.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// StringArray.java

public class StringArray {
   //Declaring instance variables
   private String arr[] = null;

   /**
   * @param arr
   */
   public StringArray(String[] arr) {
       this.arr = arr;
   }

   //This method will display the array elements
   public void displayArray() {
       for (int i = 0; i < arr.length; i++) {
           System.out.println(arr[i]);
       }
   }

   //This method will reverse the order of array elements
   public void reverseArray() {
       String temp;
       // Reverse array
       for (int index = 0; index < arr.length / 2; index++) {
           temp = arr[index];
           arr[index] = arr[arr.length - 1 - index];
           arr[arr.length - 1 - index] = temp;
       }
   }

   public int searchWord(String str) {

       for (int i = 0; i < arr.length; i++) {
           if (arr[i].equalsIgnoreCase(str)) {
               return i;
           }
       }
       return -1;
   }
}

=======================================

// Test.java

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {
       String arr[] = null;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       // Getting the input entered by the user
       System.out.print("How many names you want to enter ? ");
       int size = sc.nextInt();
  
       arr = new String[size];

       sc.nextLine();
       for(int i=0;i<size;i++)
       {
           System.out.print("Enter Name#"+(i+1)+":");
           arr[i]=sc.nextLine();
       }
       StringArray sa=new StringArray(arr);
       System.out.println("Displaying the array names :");
       sa.displayArray();
       sa.reverseArray();
       System.out.println("Displaying the array names in reverse order:");
       sa.displayArray();
      
       System.out.print("Enter name to search :");
       String name=sc.nextLine();
       int indx=sa.searchWord(name);
       if(indx==-1)
       {
           System.out.println("The name "+name+" not found in the array.");
       }
       else
       {
           System.out.println("The name "+name+" is found at position "+indx);
       }
      
   }

}

=================================

Output:

How many names you want to enter ? 5
Enter Name#1:Sachin
Enter Name#2:Ricky
Enter Name#3:Patinson
Enter Name#4:James
Enter Name#5:Billy
Displaying the array names :
Sachin
Ricky
Patinson
James
Billy
Displaying the array names in reverse order:
Billy
James
Patinson
Ricky
Sachin
Enter name to search :James
The name James is found at position 1

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Given the following problem, explain what instance variables and methods you would create, what parameters would...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access...

  • You may not add any instance variables to any class, though you may create local variables...

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. (I need step 2 please.) Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [...

  • You are going to write an object that takes a word (or short phrase) as an...

    You are going to write an object that takes a word (or short phrase) as an input for the Constructor and then will reverse the letters in the word. The object to implement this is referred to as **ReverseWord** and consists of **only** the following public methods: **public void setWord(String word)** - Sets the word to be processed. **public String getWord()** - Returns the word that is set with **setWord**. Note that if this is called before setWord then an...

  • Create two classes. Song. Should include the name of the song and the artist Playlist. Should...

    Create two classes. Song. Should include the name of the song and the artist Playlist. Should include a list of song objects from the above class, a title for the list and a description of the list. Specific method requirements below. Functionality You should be able to print both a song and a playlist. Formatting should follow the below examples EXACTLY. I should be able to print a so11ng or a playlist by calling the standard python print function. Format...

  • Choose a health problem that you would like to create a health communication campaign for. This...

    Choose a health problem that you would like to create a health communication campaign for. This campaign will take place in your neighborhood. In deciding to create the campaign, you have decided to explore what impacts the health problem you have chosen, using the ecological model. Explore each factor of the ecological model for your chosen health concern and be specific when listing components. For instance, list specific nonprofit organizations dedicated to your health concern, list specific schools in your...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing...

    CSCI 161 - Lab 2: "Give Me a Second!" Overview This lab will have you developing a program that asks the user to input a time period as a number of seconds and in turn calculates the whole number of days, hours and minutes that are contained within the entered time period. Refer to the "Output Format" below for an example of what your program must do. The objectives of this lab are to reinforce and help teach: proper Java...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT