Question

***Please give the java code for the below and add comments for different areas for a...

***Please give the java code for the below and add comments for different areas for a dummy to understand***

This java program will combine the techniques of handling arrays, decision control statements, and loops.

Your program should accomplish the following:

  1. Build an array that holds characters (size 35)
  2. Load the array with random capital letters (use random generator)
  3. Your program should present a menu to the user allowing for the following “actions”.
  1. To “search” for a target letter of the user’s choice.
    1. If found, the location (index) of the search target will be given (e.g. it is located in spot/index 8)
    2. If it is not found, a simple message saying it isn’t in the array is sufficient
  2. A “count” to see how many times a letter (given to you by the user) is in the array
  3. A “print” which will print out the contents of the array for the user

Notes for the above Java Program:

  1. This program should consist of a handful of logical modules (procedures and functions). at least 3 methods in addition to “main”.
  2. methods should really not be doing I/O – do all that and pass the information to the method(s) to be used. The methods should return the answer or appropriate information back to the calling statement for processing.
  3. the menu should allow for multiple iterations (loop it) in addition to allowing the user to quit when desired.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN JAVA:

import java.util.Scanner;
class character
{
//method to fill a character array with random capital alphabets
//we generate a number from 0 to 25 and add 65 to it, since we know ASCII of A-Z is 65-90
void fillArray(char a[])
{
for(int i=0;i<a.length;i++)
{
char c=(char)(65+(int)(Math.random()*26));
a[i]=c;
}
}
//method to find and return the index of first occurrence of a given character in given array
//returns -1 if not found
int findChar(char a[],char x)
{
for(int i=0;i<a.length;i++)
{
if(a[i]==x)
return i;
}
return -1;
}
//method to return number of times a given character occurs in a given array
int countChar(char a[],char x)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==x)
count+=1;
}
return count;
}
//method to print the given array
void printArray(char a[])
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in); //Scanner object for inputs
character ob=new character(); //creating object of class
char a[]=new char[35]; //creating empty array
ob.fillArray(a); //filling the array
int ch;
char c;
do
{
System.out.println("1. Search, 2. Count, 3. Print and any other number to quit");
System.out.print("Enter your choice: ");
ch=in.nextInt(); //user choice input
switch(ch)
{
case 1:
System.out.print("Enter the character you want to search: "); //input prompt
c=in.next().charAt(0); //input
int search=ob.findChar(a,c);
if(search==-1)
System.out.println(c+" was not found");
else
System.out.println(c+" was found at position "+search);
break;
case 2:
System.out.print("Enter the character you want to count: "); //input prompt
c=in.next().charAt(0); //input
int count=ob.countChar(a,c);
System.out.println(c+" was found in the array "+count+" times");
break;
case 3:
ob.printArray(a);
break;
default:
System.out.println("Thank you for using this system!"); //if user chose to quit
}
System.out.println();
}while(ch>=1 && ch<=3);
}
}

OUTPUT:

Blue): Terminal Window - Rik Options 1. Search, 2. Count, 3. Print and any other number to quit Enter your choice: 3 IIRCE JO

Add a comment
Know the answer?
Add Answer to:
***Please give the java code for the below and add comments for different areas for a...
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
  • Please complete this code for java Lab Write a program as follows: Create an array with...

    Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a...

    #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a list of up to 10 players and their high scores in the computer's memory. Use two arrays to manage the list. One array should store the players' names, and the other array should store the players' high scores. Use the index of the arrays to correlate the names with the scores. Your program should support the following features: a. Add a new player and...

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • Java This assignment will give you practice with line based file processing and scanner methods. Modify...

    Java This assignment will give you practice with line based file processing and scanner methods. Modify the Hours program we did in class. You are going to write a program that allows the user to search for a person by ID. Your program is required to exactly reproduce the format and behavior of the log of execution as follows: Enter an ID: 456 Brad worked 36.8 hours (7.36 hours/day) Do you want to search again? y Enter an ID: 293...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • using java Program: Please read the complete prompt before going into coding. Write a program that...

    using java Program: Please read the complete prompt before going into coding. Write a program that handles the gradebook for the instructor. You must use a 2D array when storing the gradebook. The student ID as well as all grades should be of type int. To make the test process easy, generate the grade with random numbers. For this purpose, create a method that asks the user to enter how many students there are in the classroom. Then, in each...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE...

    CAN SOMEONE PLEASE HELP ME WRITE THIS CODE IN C++, PLEASE HAVE COMMENTS IN THE CODE IF POSSIBLE!! General Description: Write a program that allows the user to enter data on their friends list. The list holds a maximum of 10 friends, but may have fewer. Each friend has a name, phone number, and email address. The program first asks the user to enter the number of friends (1-10). You are not required to validate the entry, but you may...

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