Answer:
The program to this question can be given as:
Program:
//import package for user input.
import java.util.*;
//define class
public class Budget
{
public static void main(String [] a) //define main method.
{
//creating Scanner class object.
Scanner ob =new Scanner(System.in);
//define variable.
double budgetamount=0, amountspent=0 ,difference=0,total=0,num=0 ;
int count = 0;
System.out.println("How much have you budgeted for the month? :"); //print message.
budgetamount=ob.nextDouble(); //taking input
while(budgetamount != 0) //checkig number greater then 4 digite.
{
budgetamount= budgetamount/10;
++count;
}
if(count>4) //condition
{
System.out.println("enter each expense, then type -999 to quit: ");
while(num!=-999) //taking expense
{
total=total+num; //totaling expense
num=scan.nextDouble();
}
if(total<=budgetamount) //condtion for over budget.
{
System.out.print("under budget:"); //print message
System.out.print(budgetamount-total);
}
else
{
System.out.print("over budget:"); //for under budget
System.out.print(total-budgetamount);
}
}
else
{
System.out.println("not valid"); //message for number lessthen 4 digit.
}
}
}
output:
How much have you budgeted for the month? : 1200.55.
enter each expense, then type -999 to quit: 365.89
556.90
339.98
-999
over budget:1262.77
Explanation:
The explanation of this program can be given as:
In the above program we import the package in that is used for scanner class. This class is used for the input from the user after input we use the while loop and if-else statement. The while loop is the entry control loop It is used for input validation and if-else is used for the checking condition. Then we insert expense that is inserted by the user. Then we calculate under-budgeted and over-budgeted by conditional statement that is if-else. and at the last we print it.