Saturday, December 11, 2021

Sum of Numbers Using Inheritance in Java

 Machine Problem

Write a program that prompts the user to input a positive   integer and it will loop until -1 is given and give the total sum of the   given numbers using Java programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.


My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Program Listing

import java.util.Scanner;


/*

 * Machine Problem

 * 

 * Write a program that prompts the user to input a positive 

   integer and it will loop until -1 is given and give the total sum of the 

   given numbers.

 */


class Sum_All {


int sum=0,data=0;

Scanner input = new Scanner(System.in);

 

void input_values()  {

System.out.println("\n");

     System.out.print("\tSum of Numbers Using Inheritance in Java");

     System.out.println("\n");

System.out.print( "Enter Positive Integer (the program exits if the input is -1): ");

data = input.nextInt();

}

}


class Sentinel extends Sum_All {


void Process_Data() {

while (data != -1) {

sum += data;

System.out.print("Enter Positive Integer (the program exits if the input is -1): ");

data = input.nextInt();

}

    } 


    void display() {

    System.out.println();

    System.out.println("The sum is " + sum+ ".");

input.close();

    }


    

    

    public static void main(String args[]) {

   

        Sentinel obj = new Sentinel();

        obj.input_values();

        obj.Process_Data();

        obj.display();

    }

}


No comments:

Post a Comment