I have a homework assignment that's due in a couple of days and I need a set of experienced Java eyes to confirm that a few of the answers to the questions below are correct. The hard part about these questions (for me anyways as a newbie Java person) is that each question is dependent on the previous question, so if the first parts aren't right, then the answers following prob won't be either. Any help or guidance you can provided would be GREATLY appreciated!!!


Question 1
Create a public Class called Q4. Include two member variables (an int and a float). Add a method to Q4 called printValues() that prints the values of the two member variables. (Hint: The correct answer will not have a Main method)

ANSWER

public class Q4 {
	
	//MEMBER VARIABLES
	int integer1;
	float float1;
	
	public void printValues() {
	System.out.println("integer1, float1");
	}
}

Question 2
Show a constructor for the Q4 class in Question 1. It must take two parameters that set some initial values for the member variables.

ANSWER

public class Q4 {
	
	public Q4(int integer1, float float1) {
		this.integer1 = integer1;
		this.float1 = float1;
	}

	//MEMBER VARIABLES
	int integer1;
	float float1;
	
	public void printValues() {
	System.out.println("integer1, float1");
	}
}

Question 3
Create a separate class called UseQ4 that instantiates the Q4 class from Question 1 using the constructor from Question 2. (Hint: The correct answer on this one will have a Main method)

ANSWER
Q4 myInstanceOfQ4(3,5.5);

float mytotal;
mytotal = myInstanceOfQ4.add_values();

Question 4
Add a line to UseQ4 from Question 3 that calls printValues() to display the values of Q4’s member variables.

ANSWER

myInstanceOfQ4.print_values();

Again, any help or guidance you can provided would be GREATLY appreciated!!! Thx.

For your first answer the printValues method is wrong. You printed out the words "integer1, float1", but what they want you to do is print the values of those variables.

//This is wrong, it prints the words integer1 and float1, not the values.
public void printValues() {
System.out.println("integer1, float1");
}

Your answer to question 2 is correct. Your answer to question 3 is incorrect because you never created a new class called UseQ4. Read this to learn how to create objects.

//You need this
public class UseQ4{
//your code here. You need a main method. You also need to instantiate a new Q4 Object inside of your main method. The way you instantiated the Q4 Object was incorrect.
}

This needs to use the correct function name.

Question 4
Add a line to UseQ4 from Question 3 that calls printValues() to display the values of Q4’s member variables.

ANSWER
myInstanceOfQ4.print_values();

So is this right for question 1? Eclipse seems to think so.

public class Q4 {
	
	public Q4(int integer1, float float1) {
		this.integer1 = integer1;
		this.float1 = float1;
	}

	//MEMBER VARIABLES
	int integer1 = 3;
	float float1 = 5;
	
	public void printValues() {
	System.out.println("this.integer1, this.float1");
	}
}

I think the issue I'm having with questions 3 is I can't seem to understand "instantiate a new Q4 Object inside of your main method". I think that's what throwing me off here. This is what I have so far:

public class UseQ4
{
     public static void main(String[] args){
                                          
Q4 ref = new Q4();
Q4 myInstanceOfQ4(3,5.5);

float mytotal;
mytotal = myInstanceOfQ4.add_values();
     }
}

What is this supposed to mean?

Q4 myInstanceOfQ4(3,5.5);

I think you should read about classes, methods, and Objects in the Java tutorials.

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.