Q:

java programming Final Lab Exam | csc101 PSU university

0

Write a program in java that that contains the following features:

1. A Method called $_number that takes a string then returns the number of times the character ‘$’ appears in the string. If the character is not in the string, the value 0 is returned. A suggested header for the method is:

public static int $_number (String s)

2. To test the $_number method, write the main method where the user is asked to enter a string. The string is then passed to the method which will return the number of ‘$’ occurrences in the string. The print operation is done in the main.

 

Sample run 1:

Input a string: The current gold price is $60.61 per gram and silver price is $0.72 per gram.

The number of occurrences of the character '$' is 2

 

Sample run 2:

Input a string: Java is amazing!

the character '$' does not appear in the string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
/**
 * @author Mourad Bouzegza
 * Lab Exam v1 Key answer
 */
public class LabExamKeyAnswerV1
{
 public static void main(String[] args)
 {
 Scanner in = new Scanner(System.in); 
 int number;

 System.out.println("Enter a string: ");
 String myString = in.nextLine();

 int occurences=$_number(myString);
 if(occurences==0)
 System.out.println("the character '$' does not appear in the string");
 else 
System.out.println("The number of occurences of the character '$' is "+occurences);
 }

 public static int $_number(String s)
 {
 int sum=0;
 for(int i=0;i<s.length();i++)
 if(s.charAt(i)=='$')
     sum++;
 return sum;
 }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now