Hi everyone,

I'm preparing for my Java module exam and I'm going through previous term papers. I have completed some of the questions and I need someone to check my answers and tell me if I have done it the correct way.

The questions are as below,

a) Briefly explain what byte code is.

Answer

Each java program is converted into one or more class files. The content of the class file is a set of instructions called bytecode to be executed by JVM. Java Virtual Machine (JVM) is an interpreter for the bytecode.

b) Briefly explain what is instance variable.

Answer

An instance variable is a variable defined in a class, for which each object in the class has a separate copy.

c) State the output for the below expression.

i) 9 && 33

I'm not too sure how to answer this but I know that the && logical AND operator evaluates both values to true

ii) 19 || 6

Same with one, the || logical OR operator evaluates either one of the value to true.

iii) 16 >> 2

Answer

The output is 4. Converting 16 to binary which is 10000 and shift right by 2 bits which is 100 then converting back to decimal gives 4.

iv) 9 << 1

Answer

The output is 18. Converting 9 to binary which is 1001 and shift left by 1 bit which is 10010 then converting back to decimal gives 18.

v) 34 % 6

Answer

The output is 4. The remainder operator will divide 34 by 6 and output 4 as its result.

d) Identify the mistakes found in the codes below.

public static void checkBigger(int no1, no2)

{ if (no1 > no2); 

return no1;

	else

return no2; }

}

Answer

Line 2: Return type for the second variable is not defined and there is no opening brace
Line 4: the opening brace should be after the if statement and the semi colon is not required
Line 6: void method cannot return a value
Line 8: if-else statement should be enclosed in braces
Line 10: void method cannot return a value

The above code should look like this

public static void checkBigger(int no1, int no2) 
    {
        if (no1 > no2)
        {
            do something;
        }
        
        else
        {
            do something;
        }
    }

e i) Declare a class handphone with member brand of String and price of double type

Answer

class handPhone
{
    String brand;
    double price;
}

ii) Implement a constructor to initialize all the attributes belonging to handphone object

Answer

public handPhone(String phoneBrand, double phonePrice)
{
    brand = phoneBrand;
    price = phonePrice;
}

iii) Implement a method expensiveBrand which will return true if the price of it is above 500 otherwise return false.

Answer

double price;

    public boolean expensiveBrand()
    {
        if (price > 500)
        {
            return true;
        }

        else
        {
            return false;
        }
    }

iv) Write statement to create an m2 object of handphone type and activate the expensiveBrand method

Not sure how to answer this

m2 handPhone = new m2();
m2.expensiveBrand();

m2 handPhone = new m2();
m2.expensiveBrand();

it's not correct. you shoud write:

handPhone m2 = new handPhone("some brand", 123.456);
m2.expensiveBrand();

Thanks a lot. Could you please clear my doubts regarding creating objects.

type objectName = new object(??, ??)

Is this what is required to create an object in Java?
What do they mean when they say handPhone type?
What do you call the "object" that comes after the new keyword?
What do you call these "(??, ??)" in the above example?

And, are the rest of my answers good?

>>Is this what is required to create an object in Java?
Yes, you have to instantiate every object in java.

>>What do they mean when they say handPhone type?
Its a data type just like a int is a data_type handPhone is a data_type. It just has different capabilities and properties.

>>What do you call the "object" that comes after the new keyword?
Constructor.

>>What do you call these "(??, ??)" in the above example?
Those are the parameters that the constructors take in. Sometimes
it could be nothing, and other times it could be more than zero.

Thank you for the explanation. Could you guys please tell me if my answers to the other questions are correct.

i) 9 && 33

19 || 6

It's not correct expression. In Java logical operators can be applied only to boolean type.

d) Identify the mistakes found in the codes below.

public static void checkBigger(int no1, no2)

{ if (no1 > no2);

return no1;

else

return no2; }

}

This code should look like this

public static int checkBigger(int no1, int no2)

{ if (no1 > no2) 

return no1;

else return no2; 

}

Sorry for my English, it's not native for me.

kumaran21, where did you get this questions?

This code should look like this

public static int checkBigger(int no1, int no2)

{ if (no1 > no2) 

return no1;

else return no2; 

}

Sorry for my English, it's not native for me.

kumaran21, where did you get this questions?

This is just question # 1 that was administered to us to test our progress. There is another 4 in this set. Why do you ask?

So, the void method is not appropriate for this block/method? Can you tell me why please?

Just for interest.

Method checkBigger cannot be void because it returns value. By definition void method is method which doesn't return anything.

Just for interest.

Method checkBigger cannot be void because it returns value. By definition void method is method which doesn't return anything.

Got it, I tried to adapt the behavior of the method to its declaration. So it should be the other way around.

How did I do with question E?

You answered question E correctly except creating handPhone object.

It is already has discissed.

i) Inside a class Tax, define a double constant TAXRATE of 0.05

Answer

class Tax 
{
    final double TAX_RATE = 0.05;
}

ii) Implement a method getTax which will return the value of the tax.

Answer

public double getTax()
    {
        return TAX_RATE;
    }

iii) Implement a method showTaxAmount to show the tax paid for payment amount from 1000 to 55000 with incremental of 100.

I have no idea to do question iii above. Please advice

public void showTaxAmount(int payment) {
		if (payment >= 1000 && payment <= 55000 && (payment%100 == 0) ) {
			System.out.println("Tax paid for " + payment + " is "+ payment*TAX_RATE);
		}
		else {
			System.out.println("Payment should be from 1000 to 55000 with incremental of 100");
		}
	}

Another way is to create field payment in class Tax and create constustor which initialize field payment. And then method show would not
receive argument.

//Sorry for my English, I'm just studying it:)

Hi Perveance,

To see if I understood your code, I will try and explain it below. Please correct me if I'm wrong.

Line 1: showTaxAmount method is declared void because this method does not return any values and excepts a value of int type from payment variable.

Line 2: In the if statement all three parameters must be true for Line 3 to be executed

Line 4: the else statement will be executed only when the if statement in line 2 is false.

Hi kumaran21,

You are right in your explanations.

public void showTaxAmount(int payment) {
	if (payment >= 1000 && payment <= 55000 && (payment%100 == 0) ) {
	System.out.println("Tax paid for " + payment + " is "+ payment*TAX_RATE);
		}
	else {
		System.out.println("Payment should be from 1000 to 55000 with incremental of 100");
	     }
	}

Another way is to create field payment in class Tax and create constustor which initialize field payment. And then method show would not
receive argument.

//Sorry for my English, I'm just studying it:)

Hi Perveance,

If I were to follow your suggestion to create a field payment and constructor for it, would the below suffice.

int payment;

public Tax(int taxPayment)
{
     payment = taxPayment;
}

Do I need to implement a loop with this method somewhere, maybe something like this

for (i=1000;i<=5500;i+=100)
  showTaxAmount(i);

Ofcourse someone suggested this but I don't know where I would at this code.

//Don't worry, your English is better than most people I know in my country.

Hi kumaran21,

Your constructor is correct. But I think field payment should be private. It prevents undesirable modifications.

Also I don't see the necessity to implement loop

for (i=1000;i<=5500;i+=100)
  showTaxAmount(i);

Maybe I just didn't understand you. For what did you write this loop?

Hi kumaran21,

Your constructor is correct. But I think field payment should be private. It prevents undesirable modifications.

Maybe I just didn't understand you. For what did you write this loop?

So my field payment and the constructor should look like this:

private int payment;
       public Tax(int taxPayment)
        {
       payment = taxPayment;
       }

On loop statement, someone suggested that I need this "for loop" for the part of the question that says "increments of 100".

Now field and constructor are absolutely correct.

kumaran21, I'm very sorry. Apparently I didn't understand the question.

Does method showTaxAmount() receive payment or the method just show table for all payments?

kumaran21, I'm very sorry. Apparently I didn't understand the question.

Does method showTaxAmount() receive payment or the method just show table for all payments?

Exactly my predicament as well. I don't entirely understand the question myself. My first thought was that I need a loop of some kind but I didn't know how to implement it.

I like your method better anyway, it only shows what you put in the payment field.

Hi Perveance,

I need you help to verify the codes below.

public class OddNumberLoop {

   public static int loop(int x){

       int counter = 1;
       int totalOddNo = 0;
       
        while (counter <= x){
            if (counter % 2 == 1){
                System.out.println(counter);
                totalOddNo = counter;
            }
            counter++;
        }

       return totalOddNo;
    }

    public static void main(String[] args){

        System.out.print(loop(5));
    }
}

This is the output I get


1
3
5
5

The last line of the output is suppose to be the total number of odd numbers found but instead I get the last odd number found.

How can I make it show the total number of odd number?

Hi kumaran21,

just change 12 line like this:

totalOddNo += counter;

Hi kumaran21,

just change 12 line like this:

totalOddNo += counter;

I did try that, and this is what I get

1
3
5
9

Thats the total of all the odd number. I need total number of odd numbers. So the last line should give 3 instead of 9

I'm sorry. I didn't understand your task.

Then change line 12 like this:

totalOddNo++;

I'm sorry. I didn't understand your task.

Then change line 12 like this:

totalOddNo++;

It works now, thank you so much.

Nothing at all

I'm stuck with this question.

Implement a method showHighCost will display any productno inside the factory which involve production cost above 5000.

This is my answer, is this correct?

public int showHighCost(int productionCost)
        {
            if (productionCost > 5000)
                return productNo;
        }

And I have no idea how to do the below question.

Implement a method totalCost which will return the total cost used by the factory to produce all the products.

I don't sure you method is correct. becouse I don't see the context of the task. Also I don't see the correspondence between productionCost and productNo.
I guess each Product has productionCost and Nomber (productNo).
Right? Well, how do you establish the correspondence between productNo and productionCost?

Ok, I have posted the complete question here and also my answers, please correct my answers based on your understanding of the question.

Question 3

a) Write a class Product which has String member called productno, and cost of production of double type.

Answer

class Product
{
       String productNo;
       double productionCost;
}

b) Implement a constructor to initialize the product object

Answer

public Product(String productNo, double productionCost)
{
      product = productNo;
      cost = productionCost;
}

c) Declare an array factory which produces up to 100 products for exporting to other countries.

Answer

String[] factory = new String[100];

d) Implement a method showHighCost will display any productno inside the factory which involve production cost above 5000.

Answer

public int showHighCost(int productionCost)
{
      if (productionCost > 5000)
            {
                  return productNo; 
}

e) Implement a method totalCost which will return the total cost used by the factory to produce all the products.

Answer

public int totalCost()
{
      
}

Question d & e I'm not sure how to do. Please advice, thanks.

I think factory should be an array of type Product.
So methods showHighCost and totalCosts should receive this array.

And change constructor like that:

public Product(String productNo, double productionCost)
{
      this.productNo = productNo;
      this.productionCost = productionCost;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.