Monday, May 24, 2010

Can someone pleaseeee tell me whats wrong with my code am stuckk....?

it supposed to calculate the amount due for the vehicle parked in a garage


the user enters type of car,hour arrival,minute arrival,exit hour,exit arrival then it supposed print it out in military time and charge and am not getting that any advices pleaseee thank youuu








import java.util.Scanner;


public class Assignment


{


public static void main(String [] args)


{


double hour = 0.0;


double minute = 0.0;


double cost = 0.0;


char C,c;


char V,v;





Scanner keyboard=new Scanner(System.in);


System.out.println("What kind of car do you have? C for car || V for van");





System.out.println("Enter arrival hour 0-23 ");


hour=keyboard.nextInt();





System.out.println("Enter arrival minute 0-59 ");


minute=keyboard.nextInt();





System.out.println("Enter exit hour ");


minute=keyboard.nextDouble();





System.out.println("Enter exit minute ");


minute=keyboard.nextDouble();





System.out.println("**************");


System.out.println("XXXXX");


System.out.println("**************");

Can someone pleaseeee tell me whats wrong with my code am stuckk....?
If I understand what you want properly, here is a program to complete what I think you want. Add more info if you need more help or if I misunderstood you.





// START OF CODE


import java.util.Scanner;


public class carvan


{


public static void main(String [] args)


{


// resulting values


double cost = 0.0;


boolean Van = false;


boolean Car = false;





// input data


double arrivalHours = 0.0;


double arrivalMinutes = 0.0;


double exitHours = 0.0;


double exitMinutes = 0.0;


String type; //type of vehicle





Scanner keyboard=new Scanner(System.in);


System.out.println("What kind of car do you have? C for car || V for van ");


type = keyboard.next();


if(type.charAt(0)=='C' || type.charAt(0)=='c')


Car = true;


else if(type.charAt(0)=='V' || type.charAt(0)=='v')


Van = true;





System.out.println("Enter arrival hour 0-23 ");


arrivalHours=keyboard.nextInt();





System.out.println("Enter arrival minute 0-59 ");


arrivalMinutes=keyboard.nextInt();





System.out.println("Enter exit hour ");


exitHours=keyboard.nextDouble();





System.out.println("Enter exit minute ");


exitMinutes=keyboard.nextDouble();





System.out.println("**************")...


System.out.println("XXXXX");


System.out.println("**************")...





double totalMins = ((exitHours*60.0)+exitMinutes) - ((arrivalHours*60.0)+arrivalMinutes); // find time stayed for





if( Van ) // for a van


{


if( totalMins %26gt;= 60.0*8.57 ) // if it is more than or = to 8.57 hours, the cost will be $30


cost = 30.0;


else // if parked for less than 8.57 hours


cost = 3.50 * Math.ceil(totalMins/60.0); // 3.50 per hour (and round hours up, ex 3:20 = 4 hours)


}


else if( Car ) // for a car


{


if( totalMins %26gt;= 60.0*10.0 ) // if parked for more than or = to 10 hours, the cost will be $20


cost = 20.0;


else // if parked for less than 10 hours


cost = 2.0 * Math.ceil(totalMins/60.0); // 2.00 per hour


}





System.out.printf("Parking a " + (Van?"van":"car") + " for %02d:%02d will cost $%.2f.", (int)Math.floor(totalMins/60), ((int)totalMins)%60, cost);


}


}


// END OF CODE


Please ask here if you need more help! ;P
Reply:Hello,


There is a lot wrong with this program, first the syntax is wrong. Then I am not sure the scope of your program but it should check for invalid data. Like what if I enter 53 for the hour? First get it to compile. Then you can test output and stuff.





If you can tell me exactly what you have to do, I can help you.
Reply:ahah
Reply:First, I don't think you fully grasp the concept of a char. Just as an int holds an integer and a double holds a decimal number, the char type holds characters. So, when declaring your instance variables you have written "char C, c;" and "char V, v;". Those two lines simply create variables to hold letters. Just like saying "double cost" creates a variable called "cost" that holds doubles, saying "char C" simply creates a variable called "C" that holds a character.





What this means, is that when you ask what kind of car the person has, you still need to use your Scanner to take in that char. Them simply typing a letter means nothing to your code.





Also, you made 4 char variables: C, c, v and V. I'm guessing you did this to account for case, that is, if the user types in C or c it will work. It was a noble attempt, but once again, chars don't work that way. So, what you should do is create a char variable called "car" or "vehicle", and create only one.





So replace your two lines that create char's with simply: char vehicle;.





Now, I noticed that when you ask the user what type of vehicle they have you use the "or" operator(||). Now, you could do this, but it's not necessary and most users won't know what that means. Remember, when you enclose something in double quotes ("") that's a string literal, so the computer won't read it, it will simply print it out. So a better question to ask your user is simply: "What kind of car do you have?(C for car or V for van)"





The next line of your code needs to assign the user input to your vehicle variable. So your next line of code should be this: vehicle = keyboard.nextChar();





Now the vehicle variable is holding the user input. So for now, we're good here.





Your next problem is that you assign multiple values to one variable, so only the last one shows.





Notice after you ask for arrival time, exit hour, and exit minute you assign the user input all to one variable. This won't work, because if you assign a value to a variable, it overwrites whatever was already there.





For example, let's say the user arrival minute was 27. They input 27 when prompted and 27 is assigned to the variable minute. Then when prompted they input 2 for exit hour. You have also assigned this value to the minute variable, which means that the 27 that was previously there, is overwritten! So you've lost that value. Then you do it again when asking for exit minute. The 2 that was stored in minute, is once again overwritten by the user input.





Don't worry though, this is a simple fix. Simply create 4 separate variables for arrival hour, arrival minute, exit hour, and exit minute.





So, replace "double hour" and "double minute" with this line of code: int arrivalHour, arrivalMinute, exitHour, exitMinute;





That line creates 4 int variables called arrivalHour, arrivalMinute, exitHour, and exitMinute.





Also, because these are int values, you must change your scanner to accept ints. So where you have written "keyboard.nextDouble();" you must write "keyboard.nextInt();".





Now you have 4 separate variables for arrival and exit time, and one char variable that represents car or van. We're doing well, but there's still more to be done with the rest of your code.





In this line: "double totalTime = (hour * 60.0) + minute;" you attempt to calculate the total number of minutes the user has been in the garage. Well, you have the right idea, but you aren't quite there.





How I would do this, is subtract the number of minutes past midnight that the user got there, from the number of minutes past midnight that the user left.





For instance, if the user arrived at 12:30 AM, and left at 1:45 AM, then the user arrived 30 minutes after midnight, and left 105 minutes after midnight. Therefore 105 - 30 would give you the number of minutes the user has been there.





This is easily done(oh and I'll be using int's instead of doubles since we're dealing with int's in the rest of your code: "int totalTime = (exitHour * 60 + exitMinute) - (arrivalHour * 60 + arrivalMinute);"





This code leaves us with the number of minutes the user has been there in the totalTime variable.





(We're not quite done here but I"m gonna post this to get you started and come back and finish it later)





edit:





Alright, almost done. You're next step is to check whether the vehicle is a car or a van. You had the right idea with your if statements, but you can't simply say if(van) without having a boolean variable called van.





So, a better way to do it is:


"if(vehicle == 'v' || vehicle == 'V') {


//do code for van


}else{


//do code for car


}"





In the actual if statement you're determining whether the vehicle variable holds a V or a v. Remember to enclose in single quotes('') because those are used for chars. You happen to know that if the vehicle is not a van, then it's a car. So you don't need another if statement asking if it's car, you know it is if it's not a van (so you use an else).





For the van code you need to check if the minutes are greater than 514 to assign a flat rate, or if less than 514 assign an hourly rate.





The first problem here is that instead of "if(hour %26gt;= 514)" you should write "if(totalTime %26gt;= 514)"





Then, instead of writing the opposite of that (if it's less than), you happen to know that if it's not greater than 514, it's less than. So use an else rather than a second if. And if it is less than 514, you need to calculate the number of hours the user has been there. Do this simply by writing: "cost = 3.5 * (exitHour - arrivalHour);"





For your car code, it's the same deal. Use the same adjustments you did for van in car.





Nearly done now.





Next, in your first S.o.p you need to say "vehicle" instead of "car".





In your second S.o.p you can simply say ("Exit time: " + exitHour + ":" + exitMinute);





You can then eliminate your third S.o.p.





And that's all! I'll write up the revised code for you and post it. And if you have any other questions feel free to send me an email.








edit:





Here's the revised code:





import java.util.Scanner;





public class Assignment


{


public static void main(String [] args)


{


int arrivalHour, arrivalMinute, exitHour, exitMinute;


double cost = 0.0;


char vehicle;





Scanner keyboard=new Scanner(System.in);





System.out.println("What kind of car do you have?(C for car or V for van)");


vehicle = myScanner.nextChar();





System.out.println("Enter arrival hour 0-23 ");


arrivalHour = keyboard.nextInt();





System.out.println("Enter arrival minute 0-59 ");


arrivalMinute = keyboard.nextInt();





System.out.println("Enter exit hour ");


exitHour = keyboard.nextInt();





System.out.println("Enter exit minute ");


exitMinute = keyboard.nextInt();





System.out.println("**************");


System.out.println("XXXXX");


System.out.println("**************");








int totalTime = (exitHour * 60 + exitMinute) - (arrivalHour * 60 + arrivalMinute);





if(vehicle == 'v' || vehicle == 'V') {


if(totalTime %26gt;= 514) { // more than 8.57 hours


cost = 30.0;


}else{


cost = 3.5 * (exitHour - arrivalHour); // 3.50 per hour


}





}else{


if(totalTime %26gt;= 600.0 ) { // more than 10 hours


cost = 20.0;


}else{


cost = 2 * (exitHour - arrivalHour); // 2.00 per hour


}


}





System.out.println("Parking for a" + vehicle);


System.out.println("Exit time: " + exitHour + ":" + exitMinute);


System.out.println("You need to pay : " + cost);


}


}


No comments:

Post a Comment