Thursday, December 15, 2011

Adding Binary numbers ?

// Oct 27, 2008


// Converting double to int and vice versa.


// Example for class CSE110





public class Lab6


{


public static void main(String[] args)


{





// See the program structure given at the end.





}


}





Read time using Console.readDouble method. Use "Type in time as


a decimal number" for your prompt string. Do not forget to


declare time as a variable of type double.





f. Declare hours and minutes as integers. Extract hours from time


using hours = (int)time. Obtain non-integer part of time in a


variable temp of type double. This will be in minutes, as a


decimal number. Try double temp = (time - hours)*60.0;


From temp, extract/separate integer part and store it in variable


minutes.





g. Include a statement to print hours and minutes.





h. Save your program as Lab6.java. Compile and execute. For time =


3.42, your program should print Hours = 3 Minutes = 25





3. Insert a while-loop





i Now suppose we want to run this as long as user is willing to


supply values for time. So we ask user if he/she wants to con-


tinue. If the response is Y, then we ask for a new value, else we


stop execution.





j. Declare a variable c of type char. Initialize it to value `Y',


before the loop. Place most of the code (except declarations)


inside a while loop with condition (c == 'Y').





k. At the end of loop, ask user a question. Read the response using


Console.readChar("Do you want to continue? Type Y/N ");


Read this value in variable c.





l. Save compile and run. Try different values for time and make sure


that it is printing correct values. For help see page 391 of the


textbook. It has a similar while loop.





4. Calculating seconds (Add this later)





m. We calculated hours and minutes from time. Now follow similar


steps and obtain seconds. Modify the print statement so that it


prints seconds as well.





n. This may or may not be an exact conversion to seconds. But the


error will be only in the last digit of seconds. DO NOT round to


the next higher value.





o. Save, compile and run your Lab6. Try time = 3.42 hours. When


split into 3 values, your program should print,


Hours = 3 Minutes = 25 and Seconds = 11


You must print like the example above.





Correction: Actually Java calculates double value of seconds


equal to 11.99999999999996. After casting you will get 11. This


a good example why rounding is useful.





p. Try some other value such as time = 6.249. See what is printed.


Use your calculator to verify the results.





6. Rounding time. (Add this later)





q. Now suppose we want to print a rounded integer value for time.


Remember time is of type double. Think how will you round it to


next higher integer if necessary. For example, with time = 3.42,


Rounded value of time is 3 hours. But if time = 3.74, the Rounded


value of time 4 hours. So if the fractional part is more than or


equal to 0.5 then we round it to the next higher integer, otherw-


ise we just get the integer part.





r. You just need one statement to obtain the rounded value of time.


Here again you need to cast to integer variable. Add one more


println statement that prints the rounded value. You must print


like this.


Rounded value of time is ..... hours.





7. Part-II





s. In the code, this part is after Part-I, but still inside the


while-loop. For this, read two integer variables feet and


inches. Use Console.readInt twice for this purpose. Assume that


inches will be less than equal to 11.





t. Convert feet and inches to a decimal value called length. We


want to print entire length as a decimal number in feet.


Although not necessary, you MUST first convert feet to a double


value. Use type casting. Similarly convert inches to a double


value after dividing it by 12.0. Add two double values to get


length in feet. To get full credit, explicit casting of types is


REQUIRED. For example (double)feet.





u. Insert one more println statement for printing length as a double


value.





v. Save, compile and execute. Try feet = 5 and inches = 11. Your


program should print in this format. Length in feet = 5.91666





w. Your program must have both parts, and the while-loop for con-


tinuation. Submit your program using ~cse110/submit command


before the due date (Nov 3, 11am).





Program Structure:








Declarations for part-I and part-II.





Initialization if any.





While loop starts





Part-I





Read time using Console.readDo|||Binary numbers are not referenced in your text. Binary ( base 2) was the initial form of computer programming. Adding binary numbers is done by microprocessors, some having decimal mode ( 6502), up to their programmed limits. Your request is best written in assembly language, not C.

No comments:

Post a Comment