I need help with the following please!! This is done in java. A) Identify the lo
ID: 3717255 • Letter: I
Question
I need help with the following please!! This is done in java.
A) Identify the local and global variables in the following program:
import java.util.Scanner;
class CashRegister{
private double tax;
private double total;
private double price;
public void calculateTax(double theTax){
Scanner input=new Scanner(System.in);
double thePrice
System.out.print("Enter price of item: ");
thePrice=input.nextInt();
theTax=thePrice * 0.10;
tax=theTax;
price=thePrice;
System.out.printf("The tax on $%f is $%f ", price, tax);
}
}
B) Write a method called getPrice that returns the price for the program.
Explanation / Answer
A) thePrice , theTax ARE LOCAL VARIABLES
Where as tax, price are global/instance variables
B)
import java.util.Scanner;
class CashRegister{
private double tax;
private double total;
private double price;
public static double getPrice(){
Scanner input=new Scanner(System.in);
System.out.print("Enter price of item: ");
double thePrice= input.nextInt();
return thePrice;
}
public void calculateTax(double theTax){
double thePrice=getPrice() ;
theTax= getPrice() * 0.10;
tax=theTax;
price=thePrice;
System.out.printf("The tax on $%f is $%f ", price, tax);
}
}
====================USE BELOW CODE=======================
import java.util.Scanner;
public class CashRegister{
private double tax;
private double total;
private double price;
public static double getPrice(){
Scanner input=new Scanner(System.in);
System.out.print("Enter price of item: ");
double thePrice= input.nextDouble();
return thePrice;
}
public void calculateTax(double theTax){
double thePrice=getPrice() ;
theTax= getPrice() * 0.10;
tax=theTax;
price=thePrice;
System.out.printf("The tax on $%f is $%f ", price, tax);
}
public static void main(String[] args) {
CashRegister c = new CashRegister();
Scanner input=new Scanner(System.in);
System.out.print("Enter tax: ");
double tax = input.nextDouble();
c.calculateTax(tax);
}
}