Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
theglassprison




PostPosted: Mon Sep 16, 2013 1:16 pm   Post subject: Help

Can somebody please walk me through each of the following questions? It would be much appreciated.


1. Trace through the following code segments and illustrate the output and memory.
a.
int a = 3;
int b = 9;
System.out.println(b);
a = b + 2;
a = a + b;
System.out.println(b+3);
System.out.println(a);

b.
int ans = 10;
int res = 6;
int num;
num=ans + res;
System.out.println(num + 2);
res=num + 3;
System.out.println( res);

2. To switch the values contained in the variables x and y, a programmer wrote the following segment:
x = y;
y = x;
a. If, before execution of the segment, x contains the value 7 and y contained the value 4, what value would each have after the segment was performed?
b. Rewrite the segment so that it performs the intended task correctly.

3. Circle the assignment statements that will cause errors
int a = 6;
int b = 3;
float c = 3.5;
char e = 'd';
int f;

// consider each of the following lines individually; assume the initial values given above
a = 4;
a = 4.5;
b = a + b;
c = a + b;
e = e;
e = "h";
e = "def";
b = 3 / 5;
b = f;

4. Use BEDMAS to calculate the result of the following equations.
Questions Answer
a. 240 / 8
b. 19 / 3
c. 188 % 9
d. 9 % 9
e. 5 + 8.0 / 3.0
f. 3 + (4 * (2 + 2)) % 6
g. 4 - 5 * 2 % 4 / 1
h. 4 / ?9
i. 3 % 4

5. Average.java Write a program that reads three double values and computes their average. (Don?t worry about rounding at this point).

6. Drop.java The height of an object at any given time dropped from a starting height of 100 meters is given by the equation h = 100 ? 4.9t2 where t is the time in seconds. Write a program that prompts the user for a time less than 4.5 seconds and then displays the height of the object at that time.

7. PizzaCost.java The cost of making a pizza at a local shop is as follows:
● Labour cost is $0.75 per pizza, regardless of size
● Rent cost is $1.00 per pizza, regardless of size
● Materials is $0.05*diameter*diameter (diameter is measured in inches)
Write a program that prompts that user for the size of a pizza and then display the cost of making the pizza.

8. Alive.java Write a program that calculates the number of hours of your life that you have spent sleeping. Assume that you sleep 8 hours each night. To simplify the problem, assume that there are 30 days in each month and 365 days in each year. The program output should look similar to:
Enter your birthdate:
Year: 1990
Month: 9
Day: 8
Enter today?s date:
Year: 2006
Month: 2
Day: 12
You have been alive for 5634 days.
You have slept 45072 hours.

9. Fastfood.java A fast food restaurant charges $1.69 for burgers, $1.09 for fries, and $0.99 for sodas.
a. Write a program that prompts the employee for the number burgers, fries, and sodas and then displays the totals, the PST (8%), the GST (5%), and the final cost.
b. Modify the program to prompt the employee for the amount tendered and then display the change due.

10. DigitSum.java Write a program that asks the user for a three-digit number, finds the sum of the digits of the number, and then prints both the number and its digit sum.

Reference for questions 2, 5, 10
Carter, John. An Introduction To Computer Science Using Java. Toronto: University of Toronto Press, 2003

Reference for questions 6, 7, 8, 9
Brown, Beth. A Guide to Programming in Java, 2nd Edition, for Java SE5 and Java SE6. Pennington: Lawrenceville Press


OUTPUT

Memory








Memory






OUTPUT
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon Sep 16, 2013 2:17 pm   Post subject: RE:Help

1. Trace through the following code segments and illustrate the output and memory.

In other words: What are the values of all the variables when you assign to them AND what is the output shown in the console after the program completes?

Personally, I'd use comments.

Eg:
Java:

int x = 1000;
x = x * 2; // x = 2000
System.out.println(x + 5); // OUTPUT: 2005
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: