# Thread: Some Questions - from old test papers 100 level

1. Member
Join Date
Sep 2008
Posts
3
Rep Power
0

## Some Questions - from old test papers 100 level

Ekk... I have a test tonight and am trying to make sense of all this:

Some questions that I'm not sure about are:

1. For each part (and each sub part) of this question assume:
int i = 8;
double d = 3.9;
char c = 'x' , k;

Show the value stored in the left hand side of the assignment statements below:

k = c;
k = 'c';

Write a boolean assignment statement that sets b to true when (and only when): c is not 'i' or 'o' or 'u'

2. Assume that calc is in a class that has a double data field cost, write the method so that it returns nothing, but instead stores the result in the data field.

3. Assume that calc is in a class which has double data fields called a and b. Write this method so that it takes no input, but returns the sum of the data fields multiplied by 10.

Some help (pref before tonight) would be very much appreciated.:confused:

3. Member
Join Date
Sep 2008
Posts
3
Rep Power
0

## ok.. np, these are what I think the answers might be?

1. For each part (and each sub part) of this question assume:
int i = 8;
double d = 3.9;
char c = 'x' , k;

Show the value stored in the left hand side of the assignment statements below:

k = c; --> I think this gives reference to the corresponding uni number for c?
so k = that number (what ever it might be?)

k = 'c'; --> here I think c is stored in k so k = c?

Write a boolean assignment statement that sets b to true when (and only when): c is not 'i' or 'o' or 'u' --> boolean b = true (c != 'i' || c != 'o' || c != 'u') Here I wasn't sure about using the || twice inside the expression??

2. Assume that calc is in a class that has a double data field cost, write the method so that it returns nothing, but instead stores the result in the data field.

Here I'm really unsure:
Assume: double cost;
public void calc(){ //void as it returns nothing?? is that right?
double cost // but how do I write this so it stores the result in the data field?
Any hints would be appreciated

3. Assume that calc is in a class which has double data fields called a and b. Write this method so that it takes no input, but returns the sum of the data fields multiplied by 10.
Assume:
double a;
double b;
public double calc() {
return 10*(a+b);
} // any pointers here if this is wrong would be appreciated

Thanks :)

4. Do you have a java compiler? If so, you should create a small program with these statements and after each of the put a System.out.println() to show the values of the variables.

1. For each part (and each sub part) of this question assume:
int i = 8;
double d = 3.9;
char c = 'x' , k;

Show the value stored in the left hand side of the assignment statements below:

k = c; --> I think this gives reference to the corresponding uni number for c?
so k = that number (what ever it might be?)

>>> k anc c are variables. c has been given the value 'x' a char
>>> k = c; will give the same value to k as c has (ie 'x')

k = 'c'; --> here I think c is stored in k so k = c?
>>> No k is given the value 'c', not the value of the variable c

Write a boolean assignment statement that sets b to true when (and only when): c is not 'i' or 'o' or 'u' --> boolean b = true (c != 'i' || c != 'o' || c != 'u') Here I wasn't sure about using the || twice inside the expression??

All the conditions need to be true here, not just one of them.
An or test will quit as soon as one condition is true.
An and test requires all the conditions to be true.
You are use or so it c has the value 'u', then the first test is true: c is not equal to 'i' and the full expression would return true.

2. Assume that calc is in a class that has a double data field cost, write the method so that it returns nothing, but instead stores the result in the data field.

Here I'm really unsure:
Assume: double cost;
public void calc(){ //void as it returns nothing?? is that right?
double cost // but how do I write this so it stores the result in the data field?
Any hints would be appreciated
>>> Store means to assign the value passed as an arg in a class variable.
>>> pass a double as an arg and assign it to the class variable

3. Assume that calc is in a class which has double data fields called a and b. Write this method so that it takes no input, but returns the sum of the data fields multiplied by 10.
Assume:
double a;
double b;
public double calc() {
return 10*(a+b);

>>> looks about right. Needs closing } Again write a small test program to see.

5. Member
Join Date
Sep 2008
Posts
3
Rep Power
0

## Thank you!

Hi Norm,

Thanks, I have gone over my notes again, and this time understood them a little better.

Question 2 is still annoying me a bit though, would this be correct:
double cost;
public void calc(double x){
cost = x;
}

I don't have a compiler here at home; but in class we are using drJava.

- one day I hope this all makes sense: this is my 1st time programming!

Thanks again Norm :)

6. Originally Posted by Julie
Question 2 is still annoying me a bit though, would this be correct:
double cost;
public void calc(double x){
cost = x;
}

I hope you know the basis in Java, if so no need to worry about this code segment.

In first line, you have declared a double type variable, named as cost. Is that clear?

On next three lines you have define a method name calc, which pass a double type variable. In the method assign a value to cost variable. That's it.

So where are are tuck with, better to be specific on your question.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•