In: Computer Science
Java Programming
Assignment 6-1
We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to discuss in your “reflection” the advantage of using “methods” – we discussed “re-usability” and “modularity”. Write detail comments on the program.
You need to include all the following:
Static Methods
Library methods and user written methods
Non Static Methods (Class & Object)
Methods with arguments
Methods without arguments
Conditional statements
Loops
For Example: (you could come up with any other program) - use your creativity Welcome to my “multifunction” program.
In this program I have an interactive program to show the use and benefits of “methods” in Java program. You will see a “selection menu” that would allow you to use the program. Choose 1. For your “compound interest calculator” of your “retirement nest” income, 2. For your “Interactive input of Employee attributes and then display of their attributes”, 3. For your…. Thank you for using my “multifunction” program. Have a nice day
Code is provided below along with comments. Output screenshot is provided in the last. A brief not is given about the program and the Methods. Program fullfills all conditions given in question:- loops,conditional statement,methods,with argument without argument,static methods and library methods.If need any further clarification please ask in comments.
################################################################################
Program Description:
This program ask user to provide three choice .One is to calculate salary and other is to use arithmatic calculator. and last option to exit the program. If user wants to calculate salary they can choose the same. it asks for details for finding salary and then priints the employee info with salary. If user select calculator then it gives a sub menu for different function. If user selects Addition then it promts to enter two numbers and finds addition. similarly for other operations.
Use Of Methods:- methods are used for reusability and modularity. We can use methods again and agin so we dont have to write code again and again . this shows the re usable concept of methods. Similarly we have ease of maintenance of code if we have methods.methods also provide modularity by method overloading. We can use the same name but different functionality. Hence use of methods in programming is a good practice as well as beneficial.
######################################################################
CODE---->>
import java.util.Scanner;
public class MyProgram {
//static method to multiply
public static double multiply(double x,double y)
{
return x*y;
}
//static method to add
public static double add(double x,double y)
{
return x+y;
}
//static method to subtract
public static double minus(double x,double y)
{
return x-y;
}
//static method to divide
public static double divide(double x,double y)
{
return x/y;
}
//method for salary calacuation
public static void employeeCalculator()
{
Scanner sc=new Scanner(System.in);
String name="",id="";
double wage=0;
double hours=0;
double total=0;
System.out.print("Enter the id of Employee: ");
id=sc.nextLine(); //read id
System.out.print("Name of Employee: ");
name=sc.nextLine(); //read name
System.out.print("Enter the hourly wage: $");
wage=sc.nextDouble();
System.out.print("Enter number of hours Worked: ");
hours=sc.nextDouble(); //read hours
total=multiply(hours,wage); //call multiply method to find salary
System.out.println("\nEmployee: "+name+" ID: "+id+" Hours: "+hours+" Total Salary: $"+total);
}
//main method
public static void main(String args[])
{
char choice='0';
double x=0,y=0;
Scanner s=new Scanner(System.in); //scanner to take input
while(choice!='3') //until user wants to use program
{
System.out.println("\n----What do you want to Use?----");
System.out.println("1. Press 1 for Employee Salary Calculator");
System.out.println("2. Press 2 for using Arithmetic Calculator");
System.out.println("3. Press any other key to exit");
choice=s.next().charAt(0);
if(choice=='1') //if choice is 1
{
employeeCalculator(); //call the method for employee salary
}
else if(choice=='2') //if choice is 2
{
//print diffrent options under arithmetic calculator
System.out.println(" 1. Press A for adding two numbers ");
System.out.println(" 2. Press B for Subtracting two numbers ");
System.out.println(" 3. Press C for Multiplying two numbers ");
System.out.println(" 4. Press D for Dividing two numbers ");
System.out.println(" 5. Press any other key to go to the main menu ");
choice=s.next().charAt(0);
if(choice=='1' ||choice=='2' ||choice=='3'||choice=='4') //take input if user want to use the calculator
{
System.out.print("\nEnter first number: ");
x=s.nextDouble();
System.out.print("Enter second number: ");
y=s.nextDouble();
}
if(choice=='1') //for add
{
System.out.println("Addition of "+x+" and "+y+" is: "+add(x, y));
}
if(choice=='2') //for subtract
{
System.out.println("Subtraction of "+x+" and "+y+" is: "+minus(x, y));
}
if(choice=='3') //for multiply
{
System.out.println("Multiplication of "+x+" and "+y+" is: "+multiply(x, y));
}
if(choice=='4') //for division
{
System.out.println("Division of "+x+" and "+y+" is: "+divide(x, y));
}
else //if user wants to go to main menu
{
choice='0';
continue;
}
}
else //if user want to exit the program
{
System.out.println("Thank you For Using the program!!!Have a nice Day!!");
s.close(); //closing scanner
System.exit(0); //exit
}
}
}
}
#####################################################################
OUTPUT