Question

Write a method named showChar. The method should accept two arguments: a reference to a Stringobject...

Write a method named showChar. The method should accept two arguments: a reference to a Stringobject and an integer. The integer argument is a character position within the String, with the first character being at position 0.

When the method executes, it should display the character at that character position. The method does not return anything.
Here is an example of a call to the method:

showChar("New York", 2);

In this call, the method will display the character w because it is in position 2. Demonstrate the method in a complete program.

Sample Run
java Method_showChar
Enter a line of text: The elevation of Mount Everest is 29,029 feet (8,848·in·meters)↵
Enter yourindex:3 5↵
9↵

Homework Answers

Answer #1

import java.util.Scanner;

public class Method_showChar {

public static void showChar(String a, int n)

{

System.out.println(a.charAt(n));

}

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a line of text: ");

String one = sc.nextLine();

System.out.print("Enter your index: ");

int n = sc.nextInt();

showChar(one, n);

}

}

Know the answer?
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
Similar Questions
##1.Write a function named shout. The function should accept a string argument ##and display it in...
##1.Write a function named shout. The function should accept a string argument ##and display it in uppercase with an exclamation mark concatenated to the end. ## ##2. Examine the following function header, then write a statement that calls ##the function, passing 12 as an argument. ##def show_value(quantity): ##3. Look at the following function header: ##def my_function(a, b, c): ##Now look at the following call to my_function: ##my_function(3, 2, 1) ##When this call executes, what value will be assigned to a?...
Write in C++ program and write a function named add. It should accept two const double...
Write in C++ program and write a function named add. It should accept two const double type references as arguments. The first argument should be named num1; the second argument should be named num2. The function should return by value of num1 plus num2.
Write in C++ program and write a function named computeCostPlusGST. It should accept two double type...
Write in C++ program and write a function named computeCostPlusGST. It should accept two double type variable as arguments. The first argument should be named cost; the second argument should be named rate. The argument rate should have a default value of 0.15. The function should return by value the value of cost plus the product of cost and rate
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
Must satisfy the following: Define a function named series. This function will accept two arguments, both...
Must satisfy the following: Define a function named series. This function will accept two arguments, both integers, named base and span. The function will compute the sum of the integers, starting at the value of base and down to 0 (zero) that are span apart. In other words, if base is 19 and span is 3, series will compute the sum of 19 + 16 + 13 + 10 + 7 + 4 + 1. The series function does not...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT