200 likes | 295 Views
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
E N D
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
Design • Start the program • Read product info and save in variable productInfo • Extract product name and save in variable name? • Extract product price and save in variable originalPrice$? • originalPriceRS = originalPrice$ * 3.75 • Read shipping cost and save in variable shippingCostRS • proudctPriceRS= originalPriceRS+ shippingCost+10/100 *originalPriceRS. • print the proudctPriceRS. • End the Program
Start by writing the program structure 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*///hessharraqibah//19/Feb/2013// import Section – import used java libraries publicclass Converter{ // main method publicstaticvoid 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
Variable Declaration • What variable do I need ?? • input • productPrice // in $ • shippingCost // in RS • Processing • originalPrice //in $ • price //in RS • name • productPriceRS
Variable Declaration • input • ProductInfo String • shippingCost Double // in RS • Processing • originalPrice Double //price in $ • price Double // price in RS • name String • proudctPriceRS double • any constant ?? • what about data type • input • ProductInfo • shippingCost • Processing • originalPrice • price • name • proudctPriceRS
2. Declare variable • Note: • if some variable has initial value initialize them • Use appropriate variable name // import Section – import used java libraries publicclass Converter{ // main method publicstaticvoid main( String args[] ){ // Declaration section – Declare needed variables String productInfo; String name;doubleshippingCost, price, originalPrice, proudctPriceRS;// Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class
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 importjava.util.Scanner;publicclass Converter{ // main method publicstaticvoid main( String args[] ){ // Declaration section – Declare needed variables String productInfo;//to store user input String name; //to store the product name doubleshippingCost,price,originalPrice,proudctPriceRS; Scanner input; • Each input required separate read statement • Print message before each read
4.Processing • Extract product name and save in variable name? • Extract product price and save in variable price? The input format is ProductName_xx.xx$ name=ProductName price =xx.xx
4.Processing • Extract product name and save in variable name? • 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.
Sample Run Enter the product information pen_45.70$ Enter the shipping cost 3 The product information in Saudi Riyals pen_191.5125 RS
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_infintp=productInfo.indexOf('_'); name= productInfo.substring(0,p);intl=productInfo.length(); price=Double.parseDouble(productInfo.substring(p+1,l-1));//calculate the original price originalPrice=price*3.75;//calculate proudct priceproductPriceRS=originalPrice+shippingCost+0.10*originalPrice; // Output section – Display expected results } // end main } // end class //import Section – import used java libraries importjava.util.Scanner;publicclass Converter{ // main method publicstaticvoid main( String args[] ){ // Declaration section – Declare needed variables String productInfo;//to store user input String name; //to store the product name doubleshippingCost,price,originalPrice,proudctPriceRS; Scanner input;
5. Display the result //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_infintp=productInfo.indexOf('_'); name= productInfo.substring(0,p);intl=productInfo.length(); price=Double.parseDouble(productInfo.substring(p+1,l-1));//calculate the original price originalPrice=price*3.75;//calculate proudct priceproudctPriceRS=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 //import Section – import used java libraries importjava.util.Scanner;publicclass Converter{ // main method publicstaticvoid main( String args[] ){ // Declaration section – Declare needed variables String productInfo;//to store user input String name; //to store the product name doubleshippingCost,price,originalPrice,proudctPriceRS;Scanner input; input=newScanner (System.in);
Use better display format System.out.printf("The product information in Saudi Riyals %s_%.2f RS”,name,productPriceRS);
Sample Run Enter the product information pen_45.70$ Enter the shipping cost 3 The product information in Saudi Riyals pen_191.51 RS