Question

PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods. Thank you in advance!!! Please let me know if you would like any clarification!!! ALSO please include comments if possible so I can understand better.8. Sum of Numbers in a String Write a program that asks the user to enter a series of numbers separated by commas. Here is an example of valid input: 7,9,10,2,18,6 The program should calculate and display the sum of all the numbers. 9. Sum of Digits in a String Write a program that asks the user to enter a series of single digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. (Hint: Convert the string to an array.)

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

SumOfNos.java

import java.util.Scanner;

public class SumOfNos {

public int sumOfNosSeperatedByComma(String str)

{

int sum=0;

//Converting the String to String array

String arr[]=str.split(",");

for(int i=0;i<arr.length;i++)

{

//calculating the sum

sum+=Integer.parseInt(arr[i]);

}

return sum;

}

public void sumOfIndividualDigits(String str)

{

int sum=0,num;

int high,low;

//Converting String to char array

char arr[]=str.toCharArray();

high=Integer.parseInt(String.valueOf(arr[0]));

low=Integer.parseInt(String.valueOf(arr[0]));

  

//Calculating sum and find high and lowest digit

for(int i=0;i<arr.length;i++)

{

num=Integer.parseInt(String.valueOf(arr[i]));

sum+=num;

if(high<num)

high=num;

if(low>num)

low=num;

}

  

//Displaying the output

System.out.println("Sum = "+sum);

System.out.println("Highest Digit = "+high);

System.out.println("Lowest Digit = "+low);

  

}

}

____________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

String str;

/*

* 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("Enter Nos seperated by comma :");

str=sc.nextLine();

SumOfNos son=new SumOfNos();

  

//Displaying the output

System.out.println("Sum = "+son.sumOfNosSeperatedByComma(str));

  

//Getting the input entered by the user

System.out.print("Enter numbers :");

str=sc.next();

son.sumOfIndividualDigits(str);

  

}

}

_______________________

Output:

Enter Nos seperated by comma :7,9,10,2,18,6
Sum = 52
Enter numbers :2514
Sum = 12
Highest Digit = 5
Lowest Digit = 1

___________Thank You

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...
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
  • c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two...

    c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two interesting programming challenges that will require a bit of character and/or string related processing with functions. (Carefully read the specifications for each programming challenge and select ONE.) (1) Sum of Digits in a String Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program...

  • Design a program in pseudocode that asks the user to enter a string containing a series...

    Design a program in pseudocode that asks the user to enter a string containing a series of ten single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string except for the highest value digit. You may only use one main module and one sorting function; otherwise, do not modularize your program. For example, if the user enters 5612286470, the sum would be 33. (0+1+2+2+4+5+6+6+7) If there are multiple highest...

  • 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...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

  • I need help with the following assignment: Write an application named DailyTemps that continuously prompts a...

    I need help with the following assignment: Write an application named DailyTemps that continuously prompts a user for a series of daily high temperatures until the user enters a sentinel value of 999. Valid temperatures range from -20 through 130 Fahrenheit. When the user enters a valid temperature, add it to a total; when the user enters an invalid temperature, display the error message: Valid temperatures range from -20 to 130. Please reenter temperature. Before the program ends, display the...

  • Please I need help with this c++ code. please show all steps , write comments and...

    Please I need help with this c++ code. please show all steps , write comments and show sample runs. Thank you. 1. The Federal Bureau of Investigation (FBI) has recently changed its Universal Control Numbers (UCN) for identifying individuals who are in the FBI's fingerprint database to an eight-digit base 27 value with a ninth check digit. The digits used are: 0123456789ACDE FHJKLMNPRTVWX Some letters are not used because of possible confusion with other digits: B->8, G->C, I- >1, 0->0,...

  • I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm...

    I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 13720, and the user guesses 83521, the...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

  • please I need help with the question: Problem: Write a C++ program that reads each character...

    please I need help with the question: Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits. (*The main difference between “cin >>...

  • ******** IN JAVA ********* I have a program that I need help debugging. This program should...

    ******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...

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