Some questions people had in class #7

 

How to go about writing a program, and being aware of what code to use?

 

You must figure out the steps that need to be taken, and then translate them into the language (Java). Figuring out the steps requires logical thinking. You know what the starting point is and where you want to go, and what is legal to do, and you must piece together a solution. Brainstorming, planning ahead, breaking down anything difficult into smaller tasks, and trial and error all play a part. Practice and experience are invaluable for developing this skill.

 

Where do I start on this homework you just gave us?

 

            You need to divide and conquer. If you try to solve the whole problem at once you may be intimidated. Start by listing what has to be done to complete the task (on paper). E.g. 1) Ask user for starting odometer reading and get answer; 2) Ask user for ending odometer reading and get answer; etc. Anything that seems complicated can be left in general terms to be solved later. If getting things in the right order is stalling you, ignore that for a period of brainstorming, then come back and worry about it. Once you have a complete plan, check to make sure that the order makes sense. This much should be done before lab. Once you are confident in your plan, you are ready to type things into the computer. For each step in the plan, try to translate the step in Java statement(s). It can be helpful if you type a portion of you program in and get that part to work before trying more (e.g. get starting odometer reading from the user, and display it), so you are not dealing with a lot of errors at once. Divide and Conquer!  Ask questions if necessary. Clean up errors one by one.

 

How are we to know Java without having prior experience?

 

As with learning anything new, it takes work. Read the book, come to class, ask questions. Try things out. Practice.

 

I still don’t understand the +

 

With strings, it “glues together” two strings (concatenation is the technical term). So  “Hello “ + “there “   è “Hello there “  However, if one of the items is a String, and another is a number, then the number is converted to a String before glueing. For example, if the double variable weight has a value 120, then the concatenation:  “Weight: “ + weight produces the result “Weight: 120”. The integer 120 (binary 01111000) has been retrieved from the memory referred to by the name weight, and a new value of  “120” (binary 00110001 00110010 00110000) is used in the resulting String.

 

How many strings can you put together in a row?

 

Concatenation can be repeated mutiple times, just like addition. Just as 3 + 4 + 5 è 7 + 5 è 12, so also  “Hello “ + “there “ + “Mr Jones” è “Hello there “ + “Mr Jones” è “Hello there Mr Jones”. Any number of +’s can go in sequence. (However, note that unlike addition, concatenation IS NOT commutative !!!!)

 

If a + sign is concatenation, how do you do addition in Java?

 

Using +. If the + involves at least one String, then it means concatenation. If it involves only numbers, then it means addition.

 

What is a parameter?

 

It is information that is supplied to a method in order for the method to be able to do its job. The method is a step by step procedure for carrying out some task. The parameter(s) are in some ways like Given info – they are info that must be there for the method to start (if we try to “call” the method without the correct  parameters, the compiler will complain.)

 

Is there anything else besides Strings that can be put in a parameter?

 

Yes, the person who writes the method determines what info they need in order to do the job the method must do. This could be any kind of info. For example, there is a absolute value method in the Math class (which is provided with Java, but must be imported to be used). It needs a number (double) to be passed to it.

 

Can there be more than two parameters in parentheses?

 

Yes, whatever the method expects. I wrote a method last night that needed 6 parameters. By the way, the example in class expected ONE parameter. The parameter was built on-the-fly using concatenation of a string and a number.

 

Why are there big words for little things?

 

Because the person who invented them gets more respect that way J. Seriously, a lot of the terms are derived from a related usage from real English. A parameter in English is a limit or constraint on discussion or problem solving that controls what can be considered. In computers, the parameter is essentially an input to a process that affects what happens in the process. I.e. it is fairly similar idea.

 

How do you determine what is classified as an object?

 

One rule of thumb is to look for nouns, but that is only really a start. We are looking for things that we want to keep track of. We’d like them to be things that involve more than one detail (a SSN isn’t really a significant object because there is nothing further to keep track of about the SSN. On the other hand, a person probably would be an object – details about a person will involve the SSN, name, etc). We’d also like them to have actions that must be done to / for the object (e.g. deposit money in a bank account, change the address of a person, etc). Some objects may be a little abstract, others you could touch in the world.

 

 

 

How does the whole hierarchy concept tie into writing programs?

 

            Programs in OOP are broken up into parts. Statements are grouped into methods that do one thing. Methods are included with classes. The hierarchy organizes classes so that objects in child classes can use methods from parent classes too. This will come more into focus later when we have enough background to look at real classes.

 

How does Java (allocate?) for not being able to link big objects to smaller ones?

 

            Actually, big objects can be linked to smaller objects. For example, a bank account object may be linked to (small) transaction objects. Actually, big objects can link to big or small, small can link to big or small, … it is very flexible.