Upload
jelani-holloway
View
17
Download
0
Embed Size (px)
DESCRIPTION
CSC111. Quick Revision. Problem. We want to develop a Java program to calculate the price of a product imported from the United States and to be sold in the Saudi market. The price of the product is calculated as follows - PowerPoint PPT Presentation
Citation preview
CSC111 Quick Revision
Problem
We want to develop a Java program to calculate the price of a product imported from the United States and to be sold in the Saudi market. The price of the product is calculated as follows
Price (R.S) = original price(R.S) + shipping cost +10% of original price
Original price (R.S) = Original Price ($ )* 3.75
Your program should ask the user to enter the product in the following form
ProductName_xx.xx$
Analysis
Input ProductName_xx.xx$
processing Extract the product name from input Extract the price from input calculate original price in RS Original price (R.S) = Original Price
($ )* 3.75 Price (R.S) = original price(R.S) + shipping cost +10% of original
price
Output Prductname_xx.xxR.S
Design1. Start the program
2. Read product info and save in variable productInfo
3. Extract product name and save in variable name?
4. Extract product price and save in variable originalPrice$?
5. originalPriceRS = originalPrice$ * 3.75
6. Read shipping cost and save in variable shippingCostRS
7. proudctPriceRS= originalPriceRS+ shippingCost+10/100 *originalPriceRS.
8. print the proudctPriceRS.
9. End the Program
Coding
Start the program
/*We want to develop a Java program to calculate the price of a product imported from the United States and to be sold in the Saudi market. The price of the product is calculated as follows Price (R.S) = original price(R.S) + shipping cost +10% of original price(R.S) Original price (R.S) = Original Price($)* 3.75*///hessh arraqibah//19/Feb/2013
// import Section – import used java libraries public class Converter{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables // Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class
1. Start by writing the program structure
Variable Declaration What variable do I need ??
input productPrice // in $ shippingCost // in RS
Processing originalPrice //in $ price //in RS name productPriceRS
Variable Declaration
what about data type
input ProductInfo shippingCost
Processing originalPrice price name proudctPriceRS
input ProductInfo String shippingCost Double // in RS
Processing originalPrice Double //price in $ price Double // price in RS name String proudctPriceRS double
any constant ??
// import Section – import used java libraries public class Converter{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables String productInfo; String name; double shippingCost, price, originalPrice, proudctPriceRS;
// Input section – Enter required data // Processing section – Processing Statements
// Output section – Display expected results } // end main } // end class
2. Declare variable Note:
• if some variable has initial value initialize them
• Use appropriate variable name
3.Input
Reading input from the user• 4 basic steps
Step 1: import the Scanner class: import java.util.Scanner;
Step 2 : declaring a reference variable of a Scanner Scanner input ; //we named the object read
Step 3: creating an instance of the Scanner input = new Scanner (System.in);
Step 4: use specific methods to enter data int x = input.nextInt();
input=new Scanner (System.in); //read proudct name & price in form prudctname_price$ System.out.println("Enter the product information"); productInfo=input.next(); //read shipping cost from the user System.out.println("Enter the shipping cost"); shippingCost=input.nextDouble();// Processing section – Processing Statements }
}
/// import Section – import used java libraries import java.util.Scanner;public class Converter{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables String productInfo;//to store user input String name; //to store the product name double shippingCost,price,originalPrice,proudctPriceRS;
Scanner input;
• Each input required separate read statement
• Print message before each read
4.Processing
1. Extract product name and save in variable name?
2. Extract product price and save in variable price?
The input format is
ProductName_xx.xx$
name=ProductName price =xx.xx
4.Processing
1. Extract product name and save in variable name?
2. Extract product price and save in variable price? Find the position of ‘_’?
int p= productInfo.indexOf(‘_’); extract the product name from input
name =productInfo.substring(0,p); extract the price from input
price= productInfo.substring(p+1, productInfo.length-1);
price=Double.parseDouble(productInfo.substring(p+1, productInfo.length-1));
4.Processing
3. originalPrice = price * 3.75
4. Read shipping cost and save in variable shippingCost
5. productPriceRS= originalPrice+ shippingCost+10/100 *originalPrice.
Enter the product informationpen_45.70$Enter the shipping cost3The product information in Saudi Riyals pen_191.5125 RS
Sample Run
//import Section – import used java libraries import java.util.Scanner;public class Converter{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables String productInfo;//to store user input String name; //to store the product name double shippingCost,price,originalPrice,proudctPriceRS;
Scanner input;
input=new Scanner (System.in); //read proudct name & price in form prudctname_price$ System.out.println("Enter the product information"); productInfo=input.next(); //read shipping cost from the user
System.out.println("Enter the shipping cost"); shippingCost=input.nextDouble(); // Processing section – Processing Statements //extract product name & price from product_inf int p=productInfo.indexOf('_'); name= productInfo.substring(0,p); int l=productInfo.length(); price=Double.parseDouble(productInfo.substring(p+1,l-1)); //calculate the original price originalPrice=price*3.75; //calculate proudct price productPriceRS=originalPrice+shippingCost+0.10*originalPrice; // Output section – Display expected results } // end main } // end class
//import Section – import used java libraries import java.util.Scanner;public class Converter{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables String productInfo;//to store user input String name; //to store the product name double shippingCost,price,originalPrice,proudctPriceRS;Scanner input;
input=new Scanner (System.in);
//read proudct name & price in form prudctname_price$ System.out.println("Enter the product information"); product_info=input.next(); //read shipping cost from the user
System.out.println("Enter the shipping cost"); shippingCost=input.nextDouble(); // Processing section – Processing Statements //extract product name & price from product_inf int p=productInfo.indexOf('_'); name= productInfo.substring(0,p); int l=productInfo.length(); price=Double.parseDouble(productInfo.substring(p+1,l-1)); //calculate the original price originalPrice=price*3.75; //calculate proudct price proudctPriceRS=originalPrice+shippingCost+0.10*originalPrice; // Output section – Display expected results
System.out.println("The product information in Saudi Riyals"+name+"_"+proudctPriceRS+" RS"); } // end main } // end class
5. Display the result
Use better display format
System.out.printf("The product information in Saudi Riyals %s_%.2f RS”,name,productPriceRS);
Enter the product informationpen_45.70$Enter the shipping cost3The product information in Saudi Riyals pen_191.51 RS
Sample Run