 | Glasgow University Bioinformatics forum Glasgow University Bioinformatics forum, for the MRes bioinformatics students of the University of Glasgow |
| | | Author | Message |
|---|
markj
Posts: 69 Join date: 2008-09-23
 | Subject: Lab4 code ........... Fri Sep 26, 2008 4:07 pm | |
| I should say it's not a smart way to solve the problem . Hope anyone who is interested chould post their code as well | Code: | public static void main(String[] arg){ int itemNum = 0; double perCost = 0.0; String itemName =""; boolean numValid = false; boolean costValid = false; double pay2 = 0.0; double pay1 = 0.0; int num1 = 0; int num2 = 0; Console con = new Console("Item Share"); //input of the data //number of items(itemNum),lost per item(perCost),item name(itemName) while(!numValid){ con.println("Please enter the number of the items: "); itemNum = con.readInt(); numValid = itemNum > 0; System.err.println(itemNum); } while(!costValid){ con.println("Please enter the cost per item:(£) "); perCost = con.readDouble(); costValid = perCost > 0; System.err.println(perCost); } con.println("Please enter the name of the items: "); itemName = con.readWord(); System.err.println(itemName); //Algorithms of payment and number of items they each get //payment of 1st person(pay1); of the other 2 person(pay2) //item number of 1st person(num1); of the other 2 person(num2) double totalCost = perCost * 100 * itemNum + 450; //System.err.println(totalCost); if(totalCost%3!=0){ pay2 = Math.floor(totalCost/3)/100; pay1 = (totalCost - 2*pay2*100)/100; } else{ pay1 = totalCost/100/3; pay2 = pay1; } //Why can not get integer in the following? //double a = totalCost/100 - 2*pay2; //System.err.println(a); System.err.println(pay2); System.err.println(pay1); num2 = itemNum/3; num1 = itemNum-num2*2; System.err.println(num2); System.err.println(num1); //display the result in the console String.format String s = String.format("Each of you should pay:(£)%n" + "%8.2f%n%8.2f%n%8.2f%n" + "Each of you should get%n" + "%d%n%d%n%d%n%s.",pay1,pay2,pay2,num1,num2,num2,itemName); con.println(s); |
|
|  | | seni
Posts: 6 Join date: 2008-09-18
 | Subject: my codes for the exercise lab4 Sat Sep 27, 2008 3:29 pm | |
| hey guys check out my codes after with the help of wayne i was able to compile all this together and do something good. my style mit be different import FormatIO.*; public class Ex6 { public static void main(String []args){ //variable declaration and initialization int item = 0; double cost = 0.00; boolean legal = false; boolean valid = false; Console con = new Console (); // main body starts here. while (!legal){ // taking control of my parameters using conditional statements con.println ("enter the number of item"); item = con.readInt(); legal =item >0; } while (!valid){ // same as above taking con.println ("enter item cost"); cost = con.readDouble(); valid = cost>0.00; } con.print("item name"); String name = con.readWord(); //each item cost = total cost / number of item; int totalCost = (int)(cost*item*100) + 450 ; double p1 = (double)((totalCost/3 + totalCost%3))/100; double p2 = (double)(totalCost/3)/100; System.err.println(p1); System.err.println(p2); // cost per item = (450+(cost* 100*item))/100 ; int num1 = item/3 + item%3; int num2 = item/3; System.err.println(num1); System.err.println(num2); String s =String.format("%f%n%f%n%d%n%d\n%s",p1,p2,num1,num2,name); con.println (s); } } |
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Lab4 code ........... Sat Sep 27, 2008 3:38 pm | |
| | seni wrote: | //each item cost = total cost / number of item; int totalCost = (int)(cost*item*100) + 450 ; double p1 = (double)((totalCost/3 + totalCost%3))/100; double p2 = (double)(totalCost/3)/100;} |
This part is smarter !! |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 12:19 am | |
| Hi Wei, there is a problem in your code, for instance, if you are entering 11 items which are costing 88.44 pounds, your program finds: person1: 5 items and pays 325,78 person2: 3 items and pays 325,78 person 3: 3 items and pays 325,78 so the first person has more items and is paying the same price!!! he should pay any extra!!! I have not tested the program of Seni yet. I have not finished my program, I will continue tomorrow. |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 12:34 am | |
| ok I have found the solution, I have to do further testing to be sure, and I will post my solution tomorrow. |
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 11:22 am | |
| | Nico wrote: | Hi Wei,
there is a problem in your code,
for instance, if you are entering 11 items which are costing 88.44 pounds, your program finds:
person1: 5 items and pays 325,78 person2: 3 items and pays 325,78 person 3: 3 items and pays 325,78
so the first person has more items and is paying the same price!!! he should pay any extra!!!
I have not tested the program of Seni yet. I have not finished my program, I will continue tomorrow. |
Nico,
I dont think we should worried about the problem you said based on the discription of the problem.
if the total cost could be divisible by 3, then the cost will be equally shared even if the first person get extra amount of items |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 11:49 am | |
| If you look to the wording: | Quote: | | Any items left over after sharing them out equally will be given to the first person as a reward for using the program and possibly having to pay more.. |
Well after all, your program is ok, maybe you can skeep this step.
Here is my code:
import FormatIO.Console;
/* program for common ordering * * 1) the program begins by declaring the variables we need, lines 18-22 * 2) then it is asking the user to enter the values of itemName, itemPrice and * itemNumber, lines 24-41 * 3) Then the total cost is calculated (+ cost of delivery) lines 43-45 * 4) Than it divides the price and the number of item by 3 and fill an array to know * how much each person is going to pay and how many items they each get. lines 47-72 * 5) prints the results on console, lines 76-78 */
public class Exercise4 {
public static void main(String[]args) { Console con = new Console(); String itemName = ""; double itemPrice = 0.0, totalCost = 0.0; int itemNumber = 0; double PriceAndItem[][] = new double[3][2]; //storage of the item name con.print("Enter the name of the item (single word): "); itemName = con.readWord(); //storage of the itemPrice while(itemPrice == 0 || itemPrice < 0) { con.print("Enter the price per item: "); itemPrice = con.readDouble(); } //storage of the number of item while(itemNumber == 0 || itemNumber < 0) { con.print("Enter the number of item: "); itemNumber = con.readInt(); } System.out.println(itemNumber); //Calculation of the total cost totalCost = (itemPrice * itemNumber*100) + 450; //4.5 is for the cost of delivery, but we have to multiply by 100 to get the right remainder con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100))); //Calculation of the number of items and price to pay per person if(itemNumber%3 == 0) { int number = itemNumber/3; PriceAndItem[0][0] = number; PriceAndItem[1][0] = number; PriceAndItem[2][0] = number; //System.out.println("number can be divided by 3 and each will have: "+ PriceAndItem[0][0]); } else { int number = itemNumber/3; PriceAndItem[0][0] = number + (itemNumber%3); //the first person is paying extra fees if the total cost is not divisible by 3 PriceAndItem[1][0] = PriceAndItem[2][0] = number; //System.out.println("number cannot be divided by 3 and the first person will have: "+ PriceAndItem[0][0]+"and the others: "+ PriceAndItem[1][0]+" the remainder is "+itemNumber%3); } if(totalCost%3 == 0.0) { PriceAndItem[0][1] = PriceAndItem[1][1] = PriceAndItem[2][1] = (totalCost/3.0)/100; //System.out.println("totalCost can be divided by 3, each person will pay: "+PriceAndItem[0][1]); } else { PriceAndItem[1][1] = PriceAndItem[2][1]= (int)((totalCost/3.0)/100); PriceAndItem[0][1] = (totalCost/100)-(2*PriceAndItem[1][1]); System.out.println("totalCost cannot be divided by 3"); } //prints the results on console con.println(String.format("\n\n The first person has to pay %.2f",PriceAndItem[0][1])+" pound(s)"+" and will get "+ String.format("%.0f",PriceAndItem[0][0])+" "+itemName+"(s)"); con.println(" The second person has to pay " + PriceAndItem[1][1]+" pound(s)"+" and will get "+ String.format("%.0f",PriceAndItem[1][0])+" "+itemName+"(s)"); con.println(" The third person has to pay " + PriceAndItem[2][1]+" pound(s)"+" and will get "+ String.format("%.0f",PriceAndItem[2][0])+" "+itemName+"(s)"); } }
|
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 12:36 pm | |
| I can understand what you are doing here. It is always good to think about all the possibilities. That's very good! Thanks Nico!! | Nico wrote: | If you look to the wording:
| Quote: | | Any items left over after sharing them out equally will be given to the first person as a reward for using the program and possibly having to pay more.. |
Well after all, your program is ok, maybe you can skeep this step.
Here is my code:
import FormatIO.Console;
/* program for common ordering * * 1) the program begins by declaring the variables we need, lines 18-22 * 2) then it is asking the user to enter the values of itemName, itemPrice and * itemNumber, lines 24-41 * 3) Then the total cost is calculated (+ cost of delivery) lines 43-45 * 4) Than it divides the price and the number of item by 3 and fill an array to know * how much each person is going to pay and how many items they each get. lines 47-72 * 5) prints the results on console, lines 76-78 */
public class Exercise4 {
public static void main(String[]args) { Console con = new Console(); String itemName = ""; double itemPrice = 0.0, totalCost = 0.0; int itemNumber = 0; double PriceAndItem[][] = new double[3][2]; //storage of the item name con.print("Enter the name of the item (single word): "); itemName = con.readWord(); //storage of the itemPrice while(itemPrice == 0 || itemPrice < 0) { con.print("Enter the price per item: "); itemPrice = con.readDouble(); } //storage of the number of item while(itemNumber == 0 || itemNumber < 0) { con.print("Enter the number of item: "); itemNumber = con.readInt(); } System.out.println(itemNumber); //Calculation of the total cost totalCost = (itemPrice * itemNumber*100) + 450; //4.5 is for the cost of delivery, but we have to multiply by 100 to get the right remainder con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100))); //Calculation of the number of items and price to pay per person if(itemNumber%3 == 0) { int number = itemNumber/3; PriceAndItem[0][0] = number; PriceAndItem[1][0] = number; PriceAndItem[2][0] = number; //System.out.println("number can be divided by 3 and each will have: "+ PriceAndItem[0][0]); } else { int number = itemNumber/3; PriceAndItem[0][0] = number + (itemNumber%3); //the first person is paying extra fees if the total cost is not divisible by 3 PriceAndItem[1][0] = PriceAndItem[2][0] = number; //System.out.println("number cannot be divided by 3 and the first person will have: "+ PriceAndItem[0][0]+"and the others: "+ PriceAndItem[1][0]+" the remainder is "+itemNumber%3); } if(totalCost%3 == 0.0) { PriceAndItem[0][1] = PriceAndItem[1][1] = PriceAndItem[2][1] = (totalCost/3.0)/100; //System.out.println("totalCost can be divided by 3, each person will pay: "+PriceAndItem[0][1]); } else { PriceAndItem[1][1] = PriceAndItem[2][1]= (int)((totalCost/3.0)/100); PriceAndItem[0][1] = (totalCost/100)-(2*PriceAndItem[1][1]); System.out.println("totalCost cannot be divided by 3"); } //prints the results on console con.println(String.format("\n\n The first person has to pay %.2f",PriceAndItem[0][1])+" pound(s)"+" and will get "+ String.format("%.0f",PriceAndItem[0][0])+" "+itemName+"(s)"); con.println(" The second person has to pay " + PriceAndItem[1][1]+" pound(s)"+" and will get "+ String.format("%.0f",PriceAndItem[1][0])+" "+itemName+"(s)"); con.println(" The third person has to pay " + PriceAndItem[2][1]+" pound(s)"+" and will get "+ String.format("%.0f",PriceAndItem[2][0])+" "+itemName+"(s)"); } }
|
|
|  | | Mike
Posts: 25 Join date: 2008-09-18
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 1:03 pm | |
| Hi, guys! I'm still alive. Here is my code (though it is much worse than your codes). | Quote: | //Program for purchasing
import FormatIO.*;
public class L4Ex1 { public static void main(String[] args) { Console con = new Console(); final double costDel = 4.5; //the constant cost of delivery double costItem = 0; // the cost per item (costItem) int numbItem = 0; // the number of items (numbItem) con.print("Enter the name of the item: "); String name = con.readWord(); //the name of the item boolean valid1 = false; //prevention for the wrong cost of the item while (!valid1){ con.print("Enter the cost of a single item: "); costItem = con.readDouble(); valid1 = costItem > 0; if (costItem <= 0) { con.println("You entered a wrong number (<0), try again!"); } } System.err.println(costItem); //trace the cost of the single item boolean valid2 = false; //prevention for the wrong number of items while (!valid2){ con.print("Enter the number of items: "); numbItem = con.readInt(); valid2 = numbItem >= 3; if (numbItem <3){ con.println("You entered a wrong number (<3), please try again!"); } } System.err.println(numbItem); //trace the number of items int totCost = (int)(costItem*100*numbItem + costDel*100);// calculation of the total cost System.err.println(totCost); int eachPay = totCost/3; // calculation expenses of each person double remainder = totCost%3; double firstpay = (eachPay + remainder)/100; con.println("First person should pay: " + firstpay); double each = eachPay /100; con.println("Second person should pay: " + each); con.println("Third person should pay: " + each); int eachnumber = numbItem/3; double remnum = numbItem%3; System.err.println(eachnumber); //calculation the number of items each should get if (remnum <1){ con.println("Each person should get " + eachnumber + " items"); } else if (remnum <2){ con.println("First person should get " + (eachnumber + 1) + " items"); con.println("Second and third persons should get " + eachnumber + " items each"); } else if (remnum <3){ con.println("First person should get " + (eachnumber + 2) + " items"); con.println("Second and third persons should get " + eachnumber + " items each"); } } }
|
So, I have several questions for discussion. 1) The allowed number of items. Whether they can buy only 1 or 2 items or not? In my code they can't buy less than 3 items. Nico, did you try to run your program with the number of items, e.g. 2? The result is following: the firts person will get all items (namely 2), and other persons will get nothing. |
|  | | Nico
Posts: 48 Join date: 2008-09-18
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 2:15 pm | |
| | Mike wrote: | Whether they can buy only 1 or 2 items or not? In my code they can't buy less than 3 items.
|
excellent Mihao !!!
I did not think about that.
I have modified my code:
| Code: | while(itemNumber == 0 || itemNumber < 0 || itemNumber < 3) { |
|
|  | | markj
Posts: 69 Join date: 2008-09-23
 | Subject: Re: Lab4 code ........... Mon Sep 29, 2008 2:32 pm | |
| That is a possibility we should get avoid. That's good! | Mike wrote: | Hi, guys! I'm still alive. Here is my code (though it is much worse than your codes).
| Quote: | //Program for purchasing
import FormatIO.*;
public class L4Ex1 { public static void main(String[] args) { Console con = new Console(); final double costDel = 4.5; //the constant cost of delivery double costItem = 0; // the cost per item (costItem) int numbItem = 0; // the number of items (numbItem) con.print("Enter the name of the item: "); String name = con.readWord(); //the name of the item boolean valid1 = false; //prevention for the wrong cost of the item while (!valid1){ con.print("Enter the cost of a single item: "); costItem = con.readDouble(); valid1 = costItem > 0; if (costItem <= 0) { con.println("You entered a wrong number (<0), try again!"); } } System.err.println(costItem); //trace the cost of the single item boolean valid2 = false; //prevention for the wrong number of items while (!valid2){ con.print("Enter the number of items: "); numbItem = con.readInt(); valid2 = numbItem >= 3; if (numbItem <3){ con.println("You entered a wrong number (<3), please try again!"); } } System.err.println(numbItem); //trace the number of items int totCost = (int)(costItem*100*numbItem + costDel*100);// calculation of the total cost System.err.println(totCost); int eachPay = totCost/3; // calculation expenses of each person double remainder = totCost%3; double firstpay = (eachPay + remainder)/100; con.println("First person should pay: " + firstpay); double each = eachPay /100; con.println("Second person should pay: " + each); con.println("Third person should pay: " + each); int eachnumber = numbItem/3; double remnum = numbItem%3; System.err.println(eachnumber); //calculation the number of items each should get if (remnum <1){ con.println("Each person should get " + eachnumber + " items"); } else if (remnum <2){ con.println("First person should get " + (eachnumber + 1) + " items"); con.println("Second and third persons should get " + eachnumber + " items each"); } else if (remnum <3){ con.println("First person should get " + (eachnumber + 2) + " items"); con.println("Second and third persons should get " + eachnumber + " items each"); } } }
|
So, I have several questions for discussion. 1) The allowed number of items. Whether they can buy only 1 or 2 items or not? In my code they can't buy less than 3 items. Nico, did you try to run your program with the number of items, e.g. 2? The result is following: the firts person will get all items (namely 2), and other persons will get nothing. |
|
|  | | |
| Page 1 of 1 |
| | Permissions of this forum: | You cannot reply to topics in this forum
| |
| |
| |
|