Showing posts with label Variable Declaration. Show all posts
Showing posts with label Variable Declaration. Show all posts

Monday, June 17, 2013

Java Program Variable Declaration, Numeric Expression, Assignment Statement, Input using Scanner, and Output.


Java Programming for Eclipse

Java basics (variable declaration, numeric expression, assignment statement, input using Scanner, and output).
Problem Description
A person named name purchased numberShares shares of Microsoft stock at the price of buyPrice per share and paid the stockbroker $15 transaction fee. Two weeks later, the person sold the numberShares shares at sellPrice per share and paid another $15 for the transaction. Write a Java program to calculate and display the following:
1. the dollar amount paid for the shares 2. the dollar amount of the shares sold 3. the total transaction fee paid to the broker (including both buy and sale) 4. the amount of profit (or loss) made after selling the shares.
The values of name, numberShares, buyPrice, sellPrice are input from the user. For example, if the interactive execution looks like this (user’s inputs are shown in bold):
What’s your name? Joseph How many shares bought? 250 Buy price? 28.31 Sale price? 30.79



Code for program



import java.util.Scanner;


public class Enter Your Class Here {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

   Scanner myInput = new Scanner(System.in);
  
  //declare variables
  String name;
     double numberShares, buyPrice, sellPrice, amountPurchase, amountSellPurchase, netProfit, netProfitFinal;
     int transactionFee = 15;
       
     //Prompt
     System.out.print ("What is your name? ");
     name = myInput.nextLine();
     
     System.out.print("How many shares bought? ");
     numberShares =  myInput.nextDouble();
     
     System.out.print("Buy Price? ");
     buyPrice =   myInput.nextDouble();
     
     System.out.print("Sale Price? ");
     sellPrice = myInput.nextDouble();
     
     //calculations
     amountPurchase = numberShares * buyPrice;
     amountSellPurchase = numberShares * sellPrice;
     netProfit = amountSellPurchase - amountPurchase;
     netProfitFinal = netProfit - transactionFee*2;
     
     //Display the resulting information
     System.out.println("Hello " + name + "Here is your information for your stock transaction");
  System.out.println("Number of Shares is " + numberShares);
  System.out.println("Amount of Purchase $" + amountPurchase);
  System.out.println("Amount of sell $" + amountSellPurchase);
  System.out.println("Transaction fee paid $" + transactionFee*2);
  System.out.println("Net Profit: "+ netProfitFinal);
  
  
  
  
 }

}



Java Program Constants, Variable Declarations, Arithmetic Expressions, Assignments, Input, and Format Output.



Programming Project for Eclipse

Assignment
A convenience store sells T-shirt, potato chips, and Coke. The list prices are: $18.95 for T-shirt, $1.79 for a bag of potato chips, and $2.99 for a 12-pack Coke ($1.20 deposit as well). All merchandise except Coke is on sale with 15% off the list price. T-shirt is also charged a 6% Michigan sales tax. A customer shops at the store to buy these items. Write a Java program to simulate the shopping process — interact with the custom and prints a receipt.





Code





import java.util.Scanner;

public class a {

/**
 * 
 */
 
public static void main(String[] args) {
 //TODO Audto-generated method stub
 
 
Scanner keyboard = new Scanner(System.in);
 
double shirts = 18.95;
int numOfShirts;
int numOfChips;
int numOfCoke;
double cokeTotal;
double chips = 1.79;
double coke = 2.99;
double subTotal;
double deposit = 1.20;
double preTotal;  
double MItax = 0.06; 
double totalTax; 
double total; 
double payment; 
String name;

//Prompt for the name
System.out.print("What's your name?");
name = keyboard.nextLine();
System.out.println("Welcome to Denny's Market" + name + "! We have the following items for sale:\n");
System.out.println("T-shirt $ " + shirts + " 15% off");
System.out.println("Chips $ " + chips + " 15% off");
System.out.println("Coke $ " + coke + "\n");


//Prompt for number of the shirts 
System.out.println("How many T-shirts do you want?");
numOfShirts = keyboard.nextInt(); 
System.out.println("How many bags of chips would you like?");
numOfChips = keyboard.nextInt();
System.out.println("What about 12-pack Coke?");
numOfCoke = keyboard.nextInt();

//Calculations for the program
cokeTotal = (numOfCoke * deposit) + (numOfCoke * coke);
subTotal = (((shirts * numOfShirts) + (chips * numOfChips) + (coke * numOfCoke) + (deposit * numOfCoke)));
total = (numOfShirts * shirts) *.85 + (numOfChips * chips) *.85 + cokeTotal;
preTotal = (numOfShirts *shirts) + (numOfChips * chips) + cokeTotal;
totalTax = total * MItax; 
total = total + totalTax;

System.out.println("Your total is: $" + total);
System.out.println("Please enter your payment:");
payment = keyboard.nextDouble(); 
System.out.println("\n");

System.out.println(name + ", here is your receipt:");
System.out.println("");
System.out.println("\tItem \tUnit Price \tHow Many \tCost" + "\t");
System.out.println("-----------------------------------------------------------");

System.out.println("\tT-shirt" + "\t" + shirts + "\t" + numOfShirts + "\t" + (shirts * numOfShirts));
System.out.println("\tChips" + "\t" + chips + "\t" + numOfChips + "\t" + (chips * numOfChips)); 
System.out.println("\tCoke" + "\t" + coke + "\t" + numOfCoke + "\t" + (coke * numOfCoke));
System.out.println("\tDeposit" + "\t\t" + "\t" +  + (deposit * numOfCoke));
System.out.println("\n");

System.out.println("\tSubtotal" + "\t\t" + + subTotal);
System.out.println("\tDiscount" + "\t\t" + "-" + (preTotal-total));
System.out.println("\tTax" + "\t\t" + "\t" + + totalTax);
System.out.println("\t" + "\t\t" + "\t" + "---------\n");
System.out.println("\tTotal" + "\t\t" + "\t" + + total + "\n");

//Payment
System.out.println("\tPayment" + "\t\t" + "\t" +  + payment);

//Doing the change
System.out.println("\tYourChange"+ "\t\t" + + ((payment - total)) + "\n");
System.out.println("Than you. Come Again!");
}
}