Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mac Guy Java

macrumors newbie
Original poster
Dec 6, 2013
8
0
Earth
I am a Grade 11 Computer Science Student, and I am struggling with my work. I do not understand most of the things being taught in this class, and it doesn't help that I didn't take the Grade 10 Course.

The reason for my not understanding lies in that I didn't pay much attention in class. I'm in a tough situation right now, and I would really appreciate help with my homework. Rather than just getting the answers to the questions, I would also like to know WHAT I had to do and WHY. Sorry if I seem pushy, that's not how I'm trying to reach you guys, I'm just an idiot in need of some assistance.

I have received an assignment involving methods, and I don't know how to do it. I will post the assignment and all of its details below, and I would really appreciate it if someone could answer the questions for me and tell me what they did and how I should do it. Please try to keep instructions simple, I don't have a very good knowledge of the workings of Java.

I thank you all for your assistance.

INSTRUCTIONS (The code we are working with is located below)

1. Convert findMax() into a method that returns the maximum integer. Remember to get rid of the static class variable.
2. Get rid of the i1 variable in the getInteger() method by returning the result of parseInt().
3. Make a call to your findMax() method which will calculate the maximum of 3 integers. Do not change your findMax() method to accomplish this, think about how to call your function with 3 values.
4. Create a new function to add two integers together returning the sum back to us. Use your new addition method to add two integers and then 4 integers. Print your answers out in a nicely formatted output.

CODE WE ARE WORKING WITH
Code:
import javax.swing.JOptionPane;

public class PrintMaxNumber3
{

	// the fields that belong to the entire class/object are listed next
	static int iMaxNum;

	// the behaviours that my object can have are listed below (3 of them)
	public static int getInteger()
	{
	 	int i1;

	   	String strInteger = "  ";
      	   	strInteger = JOptionPane.showInputDialog("Type in the integer value.");
       	i1 = Integer.parseInt(strInteger);

       	return i1;
	}

	public static void findMax(int num1, int num2)
	{
       	 if (num1 > num2)
            		iMaxNum = num1;
        	else
            		iMaxNum = num2;
	}

	public static void main(String[] args)
	{
	    	int iNum1, iNum2;

		iNum1 = getInteger();
		iNum2 = getInteger();
		findMax(iNum1, iNum2);
		JOptionPane.showMessageDialog( null, 
iMaxNum+" is the larger of "+iNum1+" and "+iNum2,"Maximum Number", 
	JOptionPane.PLAIN_MESSAGE);
	}
}
 
Last edited:
Be specific with your questions. This reads as "I didn't pay any attention in class so can you do my homework"

Lol exactly..
I didn't pay attention when they tried to teach me basic stuff and now Im screwed and afraid I wont pass this year.

I am not going to do your homework. Tell us where you are hitting your head against and we can give you some tips and maybe examples. So you will actually learn something ;)
 
We can't, in good conscience, do your work for you. At a certain point there are consequences for not paying attention in class. Obviously many of us having been using Java for years, so there's no explanation for why we'd do something. We'd do it because it's what you do. If you want to know how we learned it, that's different and much harder to answer. I learned what methods were in a high school class in 1999. I would hazard a guess that I learned by way of reading the book and listening to the professor.

In any event:
Any method that is static now should have that keyword removed from its declaration when you are told to do so.
If a method needs to return a value, the type it returns needs to appear before the name of the method, where you have void now.
Getting rid of i1 is just a matter of returning the parseInt statements result rather than storing it. This means placing return properly in the line with parseInt.
If you had a machine that could tell you the max of two values, and you needed the max of three, what would you ask the machine? How many times would you need to ask?
Writing a sum method should be very straight forward. The + operator will accept two operands (left, right) and evaluates to the sum of the operands. You would just need to return this result. The signature of findMax will resemble your sum method... It returns an int and accepts 2. That should help you get started.
Once you have a machine that can add two nits, how would you ask it to sum 4? How many questions do you need to ask?

As an aside, you're just learning to program AND you're learning Java. Always mention this. How do I do this in Java? Vs. How do I do this at all, I'm using Java? Are way different. With the former there's an assumption that you know what to do, but not in a given language.

I'm sorry you've found yourself in this situation. It is unlikely that we can or should bail you out. Get tutored by your teacher or a high-performing student.

-Lee
 
this is trivial stuff, if you can't do this drop the class

Code:
import javax.swing.JOptionPane;

public class PrintMaxNumber3
{

	// the behaviours that my object can have are listed below (3 of them)
	public static int getInteger()
	{
		String strInteger = JOptionPane.showInputDialog("Type in the integer value.");
		return Integer.parseInt(strInteger);
	}

	public static int findMax(int num1, int num2)
	{
		if (num1 > num2)
			return num1;
		else
			return num2;
	}

	public static int sum(int num1, int num2){
		return num1 + num2;
	}

	public static void main(String[] args)
	{
		int a = getInteger(), b = getInteger(), c = getInteger();
		int max = findMax(a, findMax(b, c));
		System.out.println(max + " is the larger of " + a + ", " + b + ", and " + c);

		int d = getInteger();
		System.out.println(a + " + " + b + " + " + c + " + " + d + " = " + sum(sum(a, b), sum(c, d)));
	}
}
 
this is trivial stuff, if you can't do this drop the class

And yet the code below will not compile (at least I don't think it will). You have at least one unchecked exception that will have to be declared as being thrown by the method that contains it...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.