1 / 20

CSC111

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

Download Presentation

CSC111

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC111 Quick Revision

  2. 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$

  3. 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

  4. 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

  5. Coding

  6. 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

  7. Variable Declaration • What variable do I need ?? • input • productPrice // in $ • shippingCost // in RS • Processing • originalPrice //in $ • price //in RS • name • productPriceRS

  8. 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

  9. 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

  10. 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();

  11. 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

  12. 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

  13. 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));

  14. 4.Processing 3. originalPrice= price * 3.75 4. Read shipping cost and save in variable shippingCost 5. productPriceRS= originalPrice+ shippingCost+10/100 *originalPrice.

  15. Sample Run Enter the product information pen_45.70$ Enter the shipping cost 3 The product information in Saudi Riyals pen_191.5125 RS

  16. 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;

  17. 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);

  18. Use better display format System.out.printf("The product information in Saudi Riyals %s_%.2f RS”,name,productPriceRS);

  19. Sample Run Enter the product information pen_45.70$ Enter the shipping cost 3 The product information in Saudi Riyals pen_191.51 RS

More Related