Glasgow University Bioinformatics forum

Glasgow University Bioinformatics forum, for the MRes bioinformatics students of the University of Glasgow
 
HomeHome  ­FAQFAQ  ­SearchSearch  ­MemberlistMemberlist  ­UsergroupsUsergroups  ­RegisterRegister  ­Log inLog in  
Share | 
 

 Lab 5 code

View previous topic View next topic Go down 
AuthorMessage
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Lab 5 code   Tue Sep 30, 2008 2:24 pm

Hi guys,

It's my solution to Lab5. Hope it's not bad. Waiting for your comments Very Happy


import FormatIO.*;

public class Lab5 {
//Exercise for week2 /extension exercise for Lab4
//input the name of item
//if item name equal "battery", "lightbulb", or "torch"
//print out the price of each item and asked the user to enter the number
//in other cases, ask the user to input the cost for each item and number of items
//calculate the cost for each person and discount when item number over 40
//increase the size of the order to 40 when it is cheaper to do so

public static void main(String[] arg){
int iNum = 0;
double iCost = 0.0;
String iName = "";
boolean numValid = false;
boolean costValid = false;

int tCost = 0;
double pay1 = 0.0;
double pay2 = 0.0;
int num1 = 0;
int num2 = 0;

Console con = new Console("Item share");

con.println("Please enter the name of item");
iName = con.readWord();
System.err.print(iName);

//if item name is battery, lightbulb, or torch
if(iName.equals("battery")||iName.equals("lightbulb")||iName.equals("torch")){
if(iName.equals("battery")){
iCost = 0.50;
con.println("Per battery cost £0.50");
}
if(iName.equals("ightbulb")){
iCost = 0.77;
con.println("Per lightbulb cost £0.77");
}
if(iName.equals("torch")){
iCost = 3.99;
con.println("Per torch cost £3.99");
}
}
else{
while(!costValid){
con.println("Please enter the cost of each item(£)");
iCost = con.readDouble();
costValid = iCost>0;
}
}

while(!numValid){
con.println("Please enter the number of item");
iNum = con.readInt();
numValid = iNum>0;
}

if(iNum<40){
tCost = (int)(iNum*iCost*100)+450;
}
else{
tCost = (int)(Math.rint(0.9*(iNum*iCost*100+450)));
}

//tCost/3=>integer+=>double
pay2 = (tCost/3)/(double)100;
pay1 = tCost/(double)100 - pay2*2;

//if iNum=40 is cheaper
double pay40 = 0.9*(iCost*40+4.5)/3;

if(pay40<pay2 && iNum<40){
iNum = 40;
num1 = 40/3+40%3;
num2 = 40/3;

tCost = (int)(Math.rint(0.9*(40*iCost*100+450)));
pay2 = (tCost/3)/(double)100;
pay1 = tCost/(double)100 - pay2*2;
String s = String.format("You will automatically be given 40 %s " +
"because a 10%% discount for items over 40\n" +
"The payment for each person should be(£):\n" +
"%-7.2f\n%-7.2f\n%-7.2f\n" +
"and each person could get\n" +
"%d\n%d\n%d\n" +
"%s",iName,pay1,pay2,pay2,num1,num2,num2,iName);
con.println(s);
}
else{
num2 = iNum/3;
num1 = iNum - num2*2;

String s = String.format("The payment for each person should be(£):\n" +
"%-7.2f\n%-7.2f\n%-7.2f\n" +
"and each person could get\n" +
"%d\n%d\n%d\n" +
"%s",pay1,pay2,pay2,num1,num2,num2,iName);
con.println(s);
}
}
}
Back to top Go down
View user profile
CM



Posts: 7
Join date: 2008-09-30

PostSubject: Re: Lab 5 code   Tue Sep 30, 2008 11:18 pm

Looks good.
Nothing obviously wrong and certainly neater than mine...
Very scrappy at this Java thing still...





import FormatIO.Console;

public class Ex2 {

// Define constants, delivery charge and number of people.
private static final int DELIVERY = 450, NUMBER_OF_PEOPLE = 3, ITEM_NUMBER_DISCOUNT_CHECK = 40;

public static void main(String[] args) {

// Define variables.
String itemName;
int itemNumber, itemsEach, firstPersonItems, itemRemainder;
double firstPersonCost, itemCost, totalCost, individualCost, costRemainder, totalCostDiscountCheck;

Console con = new Console();

// Get input for item name, quantity and cost.
con.println("Please enter the item you wish to order. (as a single word):");
itemName = con.readLine();
con.println("Please enter how many of this item you wish to order:");
itemNumber = con.readInt();
while (itemNumber < NUMBER_OF_PEOPLE) {
con.println("Please order at least three items as whole items:");
itemNumber = con.readInt();
}
// Within, costs for known items.
if (itemName.equalsIgnoreCase("battery")) {
itemCost = 50;
con.println("Batterries cost £0.50 each.");
}
else if (itemName.equalsIgnoreCase("lightbulb")) {
itemCost = 77;
con.println("Lightbulbs cost £0.77 each.");
}
else if (itemName.equalsIgnoreCase("torch")) {
itemCost = 399;
con.println("Torches cost £3.99 each.");
}
else {
con.println("Please enter the cost of one item:");
con.print("£");
itemCost = (int) con.readDouble() * 100;
while (itemCost < 1) {
con.println("Please enter an item cost of greater than £0.01:");
con.print("£");
itemCost = (int) con.readDouble() * 100;
}
}

// Calculate total cost.
totalCost = (itemCost * itemNumber) + DELIVERY;
// Within, discount on more than 40 items.
if (itemNumber >= 40) {
totalCost = (totalCost / 100) * 90;
}
else { // need to check if increasing order to get discount would be cheaper.
totalCostDiscountCheck = (itemCost * ITEM_NUMBER_DISCOUNT_CHECK) + DELIVERY;
totalCostDiscountCheck = (totalCostDiscountCheck / 100) * 90;
if (totalCostDiscountCheck < totalCost) {
totalCost = totalCostDiscountCheck;
itemNumber = 40;
con.println("There is a 10% discount on items of 40 or more items.");
con.println("The number of items you have ordered has been incresed to 40 to allow you to\ntake advantage of this offer as it decreases your order total.");
}
}

// Calculate individual costs and number of items received.
costRemainder = totalCost % 3;
individualCost = (totalCost - costRemainder) / 3;
firstPersonCost = (individualCost + costRemainder) / 100;
individualCost /= 100;
itemRemainder = itemNumber % 3;
itemsEach = itemNumber / 3;
firstPersonItems = itemsEach + itemRemainder;

// Output for amounts to pay.
if (costRemainder > 0) con.println("\nThe first person needs to pay £" + String.format("%4.2f", firstPersonCost) + ".\nThe others need to pay £" + String.format("%4.2f", individualCost) + " each.");
else con.println("\nEach person needs to pay £" + String.format("%4.2f", individualCost) + ".");

// Output for items received.
if (itemRemainder > 0) {
con.print("\nThe first person gets " + firstPersonItems + " ");
if (firstPersonItems > 1) con.print("items");
else con.print("item");
con.print(" and the others get " + itemsEach + " ");
if (itemsEach > 1) con.print("items");
else con.print("item");
con.print(" each.");
}
else {
con.print("\nEach person gets " + itemsEach + " ");
if (itemsEach > 1) con.print("items.");
else con.print("item.");
}

// Niceness...
if (itemName.equalsIgnoreCase("fish")) con.println("\n\nToday's fish is Trout A La Creme, enjoy your meal.");
else con.println("\n\nEnjoy your " + itemName);

}

}
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Lab 5 code   Wed Oct 01, 2008 11:55 am

As the in red color highlighted, I think that's very good to define constant, use method equalsIgnoreCase instead of equals.
and i think it's a smarter way to compare to total cost when item number is more than 40. this way should be much accurate tham my method.
I would say it's a good template.
What's the rest of you guys opinion?



CM wrote:
Looks good.
Nothing obviously wrong and certainly neater than mine...
Very scrappy at this Java thing still...





import FormatIO.Console;

public class Ex2 {

// Define constants, delivery charge and number of people.
private static final int DELIVERY = 450, NUMBER_OF_PEOPLE = 3, ITEM_NUMBER_DISCOUNT_CHECK = 40;

public static void main(String[] args) {

// Define variables.
String itemName;
int itemNumber, itemsEach, firstPersonItems, itemRemainder;
double firstPersonCost, itemCost, totalCost, individualCost, costRemainder, totalCostDiscountCheck;

Console con = new Console();

// Get input for item name, quantity and cost.
con.println("Please enter the item you wish to order. (as a single word):");
itemName = con.readLine();
con.println("Please enter how many of this item you wish to order:");
itemNumber = con.readInt();
while (itemNumber < NUMBER_OF_PEOPLE) {
con.println("Please order at least three items as whole items:");
itemNumber = con.readInt();
}
// Within, costs for known items.
if (itemName.equalsIgnoreCase("battery")) {
itemCost = 50;
con.println("Batterries cost £0.50 each.");
}
else if (itemName.equalsIgnoreCase("lightbulb")) {
itemCost = 77;
con.println("Lightbulbs cost £0.77 each.");
}
else if (itemName.equalsIgnoreCase("torch")) {
itemCost = 399;
con.println("Torches cost £3.99 each.");
}
else {
con.println("Please enter the cost of one item:");
con.print("£");
itemCost = (int) con.readDouble() * 100;
while (itemCost < 1) {
con.println("Please enter an item cost of greater than £0.01:");
con.print("£");
itemCost = (int) con.readDouble() * 100;
}
}

// Calculate total cost.
totalCost = (itemCost * itemNumber) + DELIVERY;
// Within, discount on more than 40 items.
if (itemNumber >= 40) {
totalCost = (totalCost / 100) * 90;
}
else { // need to check if increasing order to get discount would be cheaper.
totalCostDiscountCheck = (itemCost * ITEM_NUMBER_DISCOUNT_CHECK) + DELIVERY;
totalCostDiscountCheck = (totalCostDiscountCheck / 100) * 90;

if (totalCostDiscountCheck < totalCost) {
totalCost = totalCostDiscountCheck;
itemNumber = 40;
con.println("There is a 10% discount on items of 40 or more items.");
con.println("The number of items you have ordered has been incresed to 40 to allow you to\ntake advantage of this offer as it decreases your order total.");
}
}

// Calculate individual costs and number of items received.
costRemainder = totalCost % 3;
individualCost = (totalCost - costRemainder) / 3;
firstPersonCost = (individualCost + costRemainder) / 100;
individualCost /= 100;
itemRemainder = itemNumber % 3;
itemsEach = itemNumber / 3;
firstPersonItems = itemsEach + itemRemainder
;

// Output for amounts to pay.
if (costRemainder > 0) con.println("\nThe first person needs to pay £" + String.format("%4.2f", firstPersonCost) + ".\nThe others need to pay £" + String.format("%4.2f", individualCost) + " each.");
else con.println("\nEach person needs to pay £" + String.format("%4.2f", individualCost) + ".");

// Output for items received.
if (itemRemainder > 0) {
con.print("\nThe first person gets " + firstPersonItems + " ");
if (firstPersonItems > 1) con.print("items");
else con.print("item");
con.print(" and the others get " + itemsEach + " ");
if (itemsEach > 1) con.print("items");
else con.print("item");
con.print(" each.");
}
else {
con.print("\nEach person gets " + itemsEach + " ");
if (itemsEach > 1) con.print("items.");
else con.print("item.");
}

// Niceness...
if (itemName.equalsIgnoreCase("fish")) con.println("\n\nToday's fish is Trout A La Creme, enjoy your meal.");
else con.println("\n\nEnjoy your " + itemName);

}

}
Back to top Go down
View user profile
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: Re: Lab 5 code   Wed Oct 01, 2008 11:04 pm

Can somebody explain this to me please? I do not understand what it is.

CM wrote:

// Niceness...
if (itemName.equalsIgnoreCase("fish")) con.println("\n\nToday's fish is Trout A La Creme, enjoy your meal.");
else con.println("\n\nEnjoy your " + itemName);

}

}


Here is my code

import FormatIO.Console;

/* program for common ordering
*
* 1) the program begins by declaring the variables we need, lines
* 2) then it is asking the user to enter the values of itemName, itemPrice and
* itemNumber, lines
* 3) Then the total cost is calculated (+ cost of delivery) lines
* 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
* 5) prints the results on console, lines
*/

public class Exercise5 {

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];

//Selection of the item: enables to select the name, the price and the number of items
con.print("\nChoice A -> Battery £0.5\nChoice B -> lightbulb £0.77\nChoice C -> torch £3.99\nChoice D -> other items\n\n Enter the letter corresponding to your choice:");
char choice = con.readChar();

switch(choice)
{
case 'A': itemName = "battery"; itemPrice = 0.5; break;
case 'B': itemName = "lightbulb"; itemPrice = 0.77; break;
case 'C': itemName = "torch"; itemPrice = 3.99; break;
default: con.print("Enter the name of the item (single word): ");
itemName = con.readWord();
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 || itemNumber < 3)
{
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
double totalCost2 = (itemPrice * 40*100) + 450;
totalCost2 = totalCost2 - ((10*totalCost2)/100);
System.err.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100)));
if(itemNumber >= 40) //discount of 10%
{
totalCost = totalCost - ((10*totalCost)/100);
con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100)));
System.out.println("number of items >= 40");
}
else if(totalCost2 < totalCost)//if it is cheaper to buy 40 items, the number of items is increased
{
totalCost = totalCost2;
itemNumber = 40;
con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100))+" and the number of items has been increased to 40, it is cheaper with the discount");
System.out.println("it is cheaper to buy 40 items");
}
else //It is not cheaper to buy 40 items
{
con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100)));
System.out.println("Normal price");
}

//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)");
}
}
Back to top Go down
View user profile
CM



Posts: 7
Join date: 2008-09-30

PostSubject: Re: Lab 5 code   Thu Oct 02, 2008 8:10 am

Nico wrote:
Can somebody explain this to me please? I do not understand what it is.

CM wrote:

// Niceness...
if (itemName.equalsIgnoreCase("fish")) con.println("\n\nToday's fish is Trout A La Creme, enjoy your meal.");
else con.println("\n\nEnjoy your " + itemName);

}

}




It's nothing particularly import. It's a quote from Red Dwarf when Cat (character from the show) is ordering "fish" repeatedly from a vending machine. This is the vending machines reply...
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Lab 5 code   Thu Oct 02, 2008 1:33 pm

I am a little struggle to understand the array part. And i think the highlighted part could be simplified.
However, nothing wrong here. The program below had met the requirement of our exercise.

Nico wrote:
Can somebody explain this to me please? I do not understand what it is.

CM wrote:

// Niceness...
if (itemName.equalsIgnoreCase("fish")) con.println("\n\nToday's fish is Trout A La Creme, enjoy your meal.");
else con.println("\n\nEnjoy your " + itemName);

}

}


Here is my code

import FormatIO.Console;

/* program for common ordering
*
* 1) the program begins by declaring the variables we need, lines
* 2) then it is asking the user to enter the values of itemName, itemPrice and
* itemNumber, lines
* 3) Then the total cost is calculated (+ cost of delivery) lines
* 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
* 5) prints the results on console, lines
*/

public class Exercise5 {

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];

//Selection of the item: enables to select the name, the price and the number of items
con.print("\nChoice A -> Battery £0.5\nChoice B -> lightbulb £0.77\nChoice C -> torch £3.99\nChoice D -> other items\n\n Enter the letter corresponding to your choice:");
char choice = con.readChar();

switch(choice)
{
case 'A': itemName = "battery"; itemPrice = 0.5; break;
case 'B': itemName = "lightbulb"; itemPrice = 0.77; break;
case 'C': itemName = "torch"; itemPrice = 3.99; break;
default: con.print("Enter the name of the item (single word): ");
itemName = con.readWord();
while(itemPrice == 0 || 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 || itemNumber < 3)
//itemNumber<3
{
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
double totalCost2 = (itemPrice * 40*100) + 450;
totalCost2 = totalCost2 - ((10*totalCost2)/100);
System.err.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100)));
if(itemNumber >= 40) //discount of 10%
{
totalCost = totalCost - ((10*totalCost)/100);
con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100)));
System.out.println("number of items >= 40");
}
else if(totalCost2 < totalCost)//if it is cheaper to buy 40 items, the number of items is increased
{
totalCost = totalCost2;
itemNumber = 40;
con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100))+" and the number of items has been increased to 40, it is cheaper with the discount");
System.out.println("it is cheaper to buy 40 items");
}
else //It is not cheaper to buy 40 items
{
con.println("\nThe total cost is (including delivery): "+String.format("%.2f",(totalCost/100)));
System.out.println("Normal price");
}

//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)");
}
}
Back to top Go down
View user profile
seni



Posts: 6
Join date: 2008-09-18

PostSubject: Re: Lab 5 code   Fri Oct 03, 2008 1:50 pm

this is my own code for the lab5 exercise

public static void main(String[] arg){

int item = 0;
double cost = 0.00;
boolean legal = false;
boolean valid = false;
double totalCost=0.0;
double disntCost = 0.0;
Console con = new Console ("Sharing of items");
// main body starts here.

con.print("item name ");
String name = con.readWord();
con.println( name );
if (name.equals("battery")){
con.println("cost = 0.50");
cost = 0.5;
valid = true;
}
else if (name.equals("lightbulb")){
con.println("cost = 0.77");
cost = 0.77;
valid = true;
}
else if (name.equals("torch")){
con.println("cost = 3.99");
cost = 3.99;
valid = true;
}
else
while (!valid){ // same as above taking
con.println ("enter item cost");
cost = con.readDouble();
valid = cost>0.00;
}



while (!legal){ // taking control of my parameters using conditional statements
con.println ("enter the number of item");
item = con.readInt();
legal =item >0;
}
//each item cost = total cost / number of item;
if (item >= 40)
{
disntCost = (((cost*item*100)+ 450)/100*90/100);
System.out.println("£" + disntCost);
con.println("you will get a 10% discount and your order will cost £" +disntCost);

}
else
{
totalCost = ((cost*item*100)+ 450)/100.0 ;

System.out.println(totalCost);
con.println("your order will cost £" + " " + totalCost);
}
disntCost=((cost*40*100)+450)/100*90/100;
if(disntCost<totalCost)
{
con.println("but your order has been increased to 40 as its cheaper to do so and here is the cost");
//totalCost=disntCost;
System.out.println("£" + disntCost);
con.println("£" + disntCost);
}

}
}
Back to top Go down
View user profile
markj



Posts: 69
Join date: 2008-09-23

PostSubject: Re: Lab 5 code   Fri Oct 03, 2008 11:39 pm

I could not find any problem in your code. Just the red part might not be necessary.

seni wrote:
this is my own code for the lab5 exercise

public static void main(String[] arg){

int item = 0;
double cost = 0.00;
boolean legal = false;
boolean valid = false;
double totalCost=0.0;
double disntCost = 0.0;
Console con = new Console ("Sharing of items");
// main body starts here.

con.print("item name ");
String name = con.readWord();
con.println( name );
if (name.equals("battery")){
con.println("cost = 0.50");
cost = 0.5;
valid = true;
}
else if (name.equals("lightbulb")){
con.println("cost = 0.77");
cost = 0.77;
valid = true;
}
else if (name.equals("torch")){
con.println("cost = 3.99");
cost = 3.99;
valid = true;
}
else
while (!valid){ // same as above taking
con.println ("enter item cost");
cost = con.readDouble();
valid = cost>0.00;
}



while (!legal){ // taking control of my parameters using conditional statements
con.println ("enter the number of item");
item = con.readInt();
legal =item >0;
}
//each item cost = total cost / number of item;
if (item >= 40)
{
disntCost = (((cost*item*100)+ 450)/100*90/100);
System.out.println("£" + disntCost);
con.println("you will get a 10% discount and your order will cost £" +disntCost);

}
else
{
totalCost = ((cost*item*100)+ 450)/100.0 ;

System.out.println(totalCost);
con.println("your order will cost £" + " " + totalCost);
}
disntCost=((cost*40*100)+450)/100*90/100;
if(disntCost<totalCost)
{
con.println("but your order has been increased to 40 as its cheaper to do so and here is the cost");
//totalCost=disntCost;
System.out.println("£" + disntCost);
con.println("£" + disntCost);
}

}
}
Back to top Go down
View user profile
Nico



Posts: 48
Join date: 2008-09-18

PostSubject: Re: Lab 5 code   Sat Oct 04, 2008 12:56 pm

Just look at the declaration, it is not necessary to do so, just =0 is ok.

seni wrote:
this is my own code for the lab5 exercise

public static void main(String[] arg){

int item = 0;
double cost = 0.00; double cost = 0;
boolean legal = false;
boolean valid = false;
double totalCost=0.0;
double disntCost = 0.0;

Console con = new Console ("Sharing of items");
// main body starts here.

}
Back to top Go down
View user profile
 

Lab 5 code

View previous topic View next topic Back to top 
Page 1 of 1

Permissions of this forum:You cannot reply to topics in this forum
Glasgow University Bioinformatics forum :: IT Strand :: Programming-