|
sbucareer (m)
|
olat, I have reviewed your attached files and they meet with assignment specs,, I thought I'd let you know, good work and well done to everyone that attempted the question.
|
|
|
|
|
|
sbucareer (m)
|
Comments in Java Code Before we proceed I would like us to spent some time to learn commenting your java code. Why do we employ comments in java or any other programming language? The simple reason is that, 99.9% of software life is actually spent on maintainence. Most people that wrote the software have either left the organization for better jobs or money or some have made billions $ in lastminutes dot com business.
The truth is that we never get the opportunity to maintain our code, so documenting it is very useful for the new clueless employee that will look at your code to try and figure out what was going on in your head when you wrote the algorithm or code.
In java we have basically two types of comments. The first one //, allows you to comment one line at a time. The other one /*comments*/, allows you comment multiple lines at a time.
In java you can generate your documentation using javadoc, a java utility that allows you to generate your project documentation by supplying javadoc a class or package name.
That takes me into saying that java documentation has many tags for rendering many options. Example you should always start your java class like this
/* * @(#)Nairaland.java 1.00 22/02/2006 * * Copyright (c) 2005-2006 Nairaland, Inc. * Lagos, Nigeria. * All rights reserved. * * This software is the confidential and proprietary information of Nairland Inc, * ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Nairaland. */
public class Nairaland extends Object{
}//End Nairaland class
You should have probably noticed these tags @(#) they have specail meaning to find out more visit here
|
|
|
|
|
|
sbucareer (m)
|
Explaining the meaning of public static void main (String[] args)
In every program there must be an entry point into the compiler, in java main is the name of that main public static void main (String[] args) The method take one array String type. I will explain later why it take this array.
When we get to concurrent programming we shall see why every program must have a single entry point to the system. A simple example is our window XP etc. We have so many things running and it all looks like there are running at the same time. The CPU can NEVER run to processes at the same time it uses a mechanism called semaphore to control every process in the system. The CPU is so fast that it uses some nanosecond to process every instruction placed in the queue buffer.
Java is a sequentail program, which does things sequence by sequence. JVM sees every program as a thread. Every thread must have a main method that is the only way java can know that the code is going to actually execute instruction not just compile.
One thing you should memorise is that main method. It is very important that you can write that main method when someone wakes you up from sleep. Remember it is the first thing that the JVM looks for when executing instruction.
|
|
|
|
|
|
sbucareer (m)
|
Now that we have managed to design and achieve Temperature class and tested it with our TemperationDemonstration class. I would like us to model the temperature in Lagos called LagosTemperature. We are going to extends our LagosTemperature to Temperation. Remember that our base class Temperature was extended to Object.
What I want you to do is to extends the LagosTemprature class to Temperature. Implement these two methods, no attributes or protected method in LagosTemperature as public methods.
1. sunny 2. rainy
When it is sunny the temperature is plus 10 and when it is rainy the temperature is minus 10 hence +10 and -10 respectively. Here I will use the Radio class to give you just snippets example. Remember don't just copy the tutorial example and submit for your work as you are only deceiving yourself.
|
|
|
|
|
|
sbucareer (m)
|
Here is example of Radio class. I have extended LagosRadio to Radio class and have added two methods to it called FM and AM, notice that there is no attributes and protected method. The LagosRadio has two constructors. One has no parameter passed to it (default constructor) and the other has two parameters passed to it (Alternative constructor).
I will write the code for this class. I will expect you to do the same for the Temperature assignment and provide a LagosTemperature and a LagosTemperatureDemonstration class with a Class Diagram of LagosTemperature.
|
|
|
|
|
|
sbucareer (m)
|
Code Snippets
/* * @(#)LagosRadio.java 1.00 22/02/2006 * * Copyright (c) 2005-2006 Nairaland, Inc. * Lagos, Nigeria. * All rights reserved. * * This software is the confidential and proprietary information of Nairland Inc, * ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Nairaland. */
public class LagosRadio extends Radio{
/* *Default constructor loads the frequency and volume to zero values * */ public LagosRadio ( ){ this(0, 0); }//End LagosRadio default Constructor
/* *Alternative constructor loads the frequency and volume to initial values * */ public LagosRadio (int freq, int vol ){ super(freq, vol); }//End LagosRadio alternetive Constructor
/* *Set the Frequency to the FM channel * */ public void FM (){ int temp = this.getFrequency( ); temp += 10; this.setFrequency (temp); }//End FM method
/* *Set the Frequency to the AM channel * */ public void AM (){ int temp = this.getFrequency( ); temp -= 10; this.setFrequency (temp); }//End AM method }//End LagosRadio class
You get the gist. Now, I want you to to do the Temperature class above providing the Class Diagram and LagosTemperature and LagosTemperatureDemonstration classes to prove that you understand the concept I will be busy putting up tutorials that will help you through the assignment, good luck. I hope to see people do this as quickly as possible.
|
|
|
|
|
|
sbucareer (m)
|
Let me explain what is happening here. Remember the Radio class we coded ealier, it has frequency attribute and volume attribute.
Let me give this scenario, You know you Dad is the Senior (Super) and you are the Junior(this). Hence you are extended to your Dad not your Dad extended to you. What we mean by extended is that some attributes about your Dad you share them i.e. skin color, eyes, nose, height, weight etc. But there are some things about your Dad you cannot share i.e. your mum (Dads wife), bank account, car, watch etc. these must be declared as private the public attributes of your Dad you can share it.
In a class when we extend a child to another class, theoretically that class becomes its Dad, and all the private attributes can not be seen by the child class but all the public and protected attributes can be seen and shared by the child class.
The protected attributes and method are only seen by the child the family of that class (In the same package) can share the protected attributes or methods. Don't worry about this I will talk about it later.
The default construction try to initialize itself to zero i.e this(0,0). Remember that the child class LagosRadio do not have attributes. The parent super (Radio) has two attributes namely
1. frequency 2. volume
We use super(freq, vol) to set the parent attributes. When you instantiate the object of LagosRadio with say 20, 3. The Super class (Radio) will have the values of 20, 3 respectively, now you can call its public method getFrquency and setFrequency, which will return 20 and allow you to change the frequency by calling setFrequecy.
This is possible because Radio and LagosRadio are Father and son and share common attributes that are public simple.
|
|
|
|
|
|
skima (m)
|
@sbucareer The Radio,RadioDemonstration did not compile. though i tried to fix some of the error but i think u should look into it very well so other student don't encounter it any more
|
|
|
|
|
|
skima (m)
|
@sbucareer u have not replied to my mails. wats the latest about my requests.
|
|
|
|
|
|
sbucareer (m)
|
Skima, sorry I have fixed the bug you can now try it out.
Also make sure you set your CLASSPATH to your working directory ok, it may not matter but some people have reported that they can't compile the class.
Example if you saved your Radio and RadioDemonstration in a directory like C:\tutorial\java then set your Classpath in your control panel -->systems-->Advanced button---> Environment Variables to
%All your system paths%;C:\tutorial\java
|
|
|
|
|
|
sbucareer (m)
|
gbengaijot, you started this post and was enthusiastic about java but I have not seen your assignment yet. Is there any problems? Let me know I will be glad to help. I would like to see you own work.
"Those people that graduate from colleges/universities are not necessary the most brilliant of all, it is just that they put the required minimal effort in their study" - by Sbucareer, 2006
|
|
|
|
|
|
sbucareer (m)
|
/* * @(#)LagosRadioDemonstration.java 1.00 22/02/2006 * * Copyright (c) 2005-2006 Nairaland, Inc. * Lagos, Nigeria. * All rights reserved. * * This software is the confidential and proprietary information of Nairland Inc, * ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Nairaland. */
import java.lang.Object;
public class LagosRadioDemonstration extends Object{
//We are going to create Object(s) of class Radio static private LagosRadio firstObject = null; static private LagosRadio secondObject = null;
public static void main (String args[]){
//We are going to instantiate two objects we created above firstObject = new LagosRadio (); secondObject = new LagosRadio ();
//Testing the new Objects or querying their attributes System.out.println (firstObject.getFrequency()); System.out.println(secondObject.getFrequency());
//Changing the identity of the objects we create above, hence assigning new values to their attribute. firstObject.setFrequency(3); secondObject.setFrequency(10);
//Testing the new Objects or querying their attributes System.out.println (firstObject.getFrequency()); System.out.println(secondObject.getFrequency()); }///End main }//End RadioDemonstration class
Here is the class for LagosRadioDemonstration. Play around with it and learn what it does. Try and change the LagosRadio class default constructor to this(10, 20) compile and run and see the difference, just generally play with it.
|
|
|
|
|
|
sbucareer (m)
|
Operator Use Description
| + | op1 + op2 | Adds op1 and op2; also used to concatenate strings | | * | op1 * op2 | Multiplies op1 by op2 | | / | op1 / op2 | Divides op1 by op2 | | % | op1 % op2 | Computes the remainder of dividing op1 by op2 |
- op1 - op2 Subtracts op2 from op1
Operator Use Description
++ op++ Increments op by 1; evaluates to the value of op before it was incremented ++ ++op Increments op by 1; evaluates to the value of op after it was incremented -- op-- Decrements op by 1; evaluates to the value of op before it was decremented -- --op Decrements op by 1; evaluates to the value of op after it was decremented > op1 > op2 Returns true if op1 is greater than op2 >= op1 >= op2 Returns true if op1 is greater than or equal to op2 < op1 < op2 Returns true if op1 is less than op2 <= op1 <= op2 Returns true if op1 is less than or equal to op2 == op1 == op2 Returns true if op1 and op2 are equal != op1 != op2 Returns true if op1 and op2 are not equal
&& op1 && op2 Returns true if op1 and op2 are both true; conditionally evaluates op2 || op1 || op2 Returns true if either op1 or op2 is true; conditionally evaluates op2 ! !op Returns true if op is false
& op1 & op2 Returns true if op1 and op2 are both boolean and both true; always evaluates op1 and op2; if both operands are numbers, performs bitwise AND operation
| op1 | op2 Returns true if both op1 and op2 are boolean and either op1 or op2 is true; always evaluates op1 and op2; if both operands are numbers, performs bitwise inclusive OR operation
^ op1 ^ op2 Returns true if op1 and op2 are different — that is, if one or the other of the operands, but not both, is true
|
|
|
|
|
|
sbucareer (m)
|
I know most of us have left school 5-20 years ago but it will worth remembering our binary, hence OR, AND, OR(XOR). You maybe wondering why do I need all these school maths? Yes we need it because the next lesson will focus on evaluation of attributes.
Function OR
Bit in op1 Corresponding Bit in op2 Result
0 0 0 1 0 1 0 1 1 1 1 1
Function AND
Bit in op1 Corresponding Bit in op2 Result
0 0 0 1 0 0 0 1 0 1 1 1
Function OR(XOR)
Bit in op1 Corresponding Bit in op2 Result
0 0 0 1 0 1 0 1 1 1 1 0
Yo need to understand this for the next tutorial. This is the way I did it for myself. Let take the AND. A girl told you she would go out with you if you have a car AND money, now
Would the girl go out with you if you came with car but no money? Hence 1 0 0 No Would the girl go out with you if you did not come with car but came with money? 0 1 0 No Would the girl go out with you if you did not come with money nor car? 0 0 0 No Would the girl go out with you if you came with both? 1 1 1 Yes, she would.
AND evaluates two things to be true. If one is false all is false. Imagine later in the tutorial when we would be evaluating two attributes that holds someone's username and passwd, we want the evaluation to return true only when the two attributes are true. i.e when opr1 = 1 and opr2 is 1 result=1. Remember 1 is true and 0 is false.
Take the same scenario to OR, the girl want either car OR money, so if you come with one she will agree. If you came with both bonus to she. But she will not agree if you did not come with none.
The OR(XOR) is that anywhere the operand is different the answer is 1 else 0
1 0 1 ( 1 OR(XOR) 0) is different so the r3sult is 1 0 1 1 (0 OR(XOR) 1) is different so the result is 1
0 0 0 because both are the same (0 OR(XOR) 0) = 0 1 1 0 because both are the same (1 OR(XOR) 1) = 0
Try and understand these things it is very important.
|
|
|
|
|
|
sbucareer (m)
|
If your direction with Java is towards Game programming you need to understand Shift and Bitwise Operators.
Operator Use Description
<< op1 << op2 Shifts bits of op1 left by distance op2; fills with 0 bits on the right side >> op1 >> op2 Shifts bits of op1 right by distance op2; fills with highest (sign) bit on the left side >>> op1 >>> op2 Shifts bits of op1 right by distance op2; fills with 0 bits on the left side
This is to do with bits manipulation. Shiftting binary bits.
Example
13 >> 1 = 6
Because 13 in binary is 1101, shiftting 1bit from the right will become 110, zero is always added in front of the shifted binary therefore it comes 0110, which is 6 in decimal
Reference ---------------------------- 1. Sun website
|
|
|
|
|
|
Viper (m)
|
even though i have gone past all these elementary java programming, i would say u are a good guy and i appreciate what u're doing.  anyway, when u come online try and come on msn chat so we go talk as i am free today.
|
|
|
|
|
|
gbengaijot (m)
|
Can sun java studio creator be used for this tutorial?,
|
|
|
|
|
|
gbengaijot (m)
|
I mean in place of java compiler 1.5 "sbu, i downloaded java compiler and its giving me the application server. i did this thrice but still thesame thing, i think maybe i am missing the point. I am off to london this weekend anyway, my PC has been acting funny since the download and reinstallation. The cursor hangs and wenever i tried moving the touch pad it comes on for like 20 seconds and hangs again. I am willing to do all this but mmmmmm, if u av a mobile number( preferably T_mobile, i can give u a call and see if we can talk or meet sometimes, what do u think is wrong with my laptop then.i use a compaq presario 1200 with 128 MB(although i was going to change the RAM this weekend) but no point since its not working again properly. i reinstalled the Windopws XP but still thesame thing.
|
|
|
|
|
|
Seun (m)
|
Hello gbenga, didn't we download the Java+Netbeans stuff together?  (sorry for interrupting)
|
|
|
|
|
|
gbengaijot (m)
|
Seun, we did, i installed it and the next thing is the applicastion server software that is in my root directory
|
|
|
|
|
|
Seun (m)
|
Check your Start Menu. Java/Netbeans should be there. Unless you installed a different one from the link I gave you.
|
|
|
|
|
|
gbengaijot (m)
|
Mmmmmmmmmmmmm, its not in there seun, Thats why i don't like learning off forums, because i feel like a dummy when i don't get along with a tutorial. i know i am only missing a point with it. Anyway, i will do it again on monday as i am travelling for my sisters wedding in london and wont be bak until sunday night. Thanks seun, ( don't ask me if i am planning to desing a forum again.lol, ) SBU, waiting for your email/post
|
|
|
|
|
|
sbucareer (m)
|
Gbenga, since you are coming to London for the weekend, try and meet me at Presidentail Restaurant at Old Kent Road, the treat is on me. It will be nice to actually meet someone from Nairaland, and generally to discuss some issues regarding java and installing IDE on your computer, if you have Laptop you can bring it along.
I will be at presidentail restaurant Friday/Saturday from 8pm to 1am. That is were I hang out for my weekends. See you in London. You can call me on 07949743363. No Nuisance calls please.
|
|
|
|
|
|
skima (m)
|
@sbu u havent replied to my mail on our last chat. which i sent to your gmail account .
|
|
|
|
|
|
White lady (f)
|
Hmmm . . . I like this. I like it when Nairalanders meet one another. So Gbenga and Sbucareer, please tell us how your 'meeting' went. I 'm eager to know!
|
|
|
|
|
|
sbucareer (m)
|
Skima, if it was your message regarding this I have answered you here. Sorry if I seem to miss your message(s) it is not on purpose.
Anyway have you started the new assignment? You know the hand-in date is this week Tuesday. How far are you getting on with the tutorial? Let me know if you are having any problems.
@Gbenga, you never phoned again? I was waiting for your call on Saturday, anyway I hope you are fine. I have not seen your last week assignment? PM if you have encountered any problem(s) and I would like to see your assignment for this week
Regards,
Valentine
|
|
|
|
|
|
sbucareer (m)
|
@White lady, Gbenga called me on Friday and told me he was tired as he just arrived to London. I told him to get some rest that we should probably meet on Saturday and he told me that he was going to a wedding @ Elephant & Castle near Presidental Suya Restaurant at Old Kent Road, I guess he must have enjoyed himself and forgot to call me.
Anyway, I hope he is ok though. I'd my digital Camera with me on Saturday to take pictures and post it in the forum but I guess it will be for another day.
|
|
|
|
|
|
White lady (f)
|
@sbucareer Aww! I wish you guys had met though. I could just imagine you waiting for him to show up. Anyway, all is well that ends well. I sincerely hope Gbenga is fine too.Thanks for taking time to reply me. Hope you had a swell weekend despite the disappointment.
|
|
|
|
|
|
skima (m)
|
i was also talking about the software/book u said u will send me. You aint saying anything about it.
As to the assignment, am workin on it. i will submitt it late tuesday.
|
|
|
|
|
|
gbengaijot (m)
|
@sbu, sorry i wasnt able to make it. I had to stay at the party till late and i culdnt call as my phone battery went down flat. However i am coming to london for a seminar presentation in central london. The African diaspora youth forum has invited me to empower young people in motivating them towards sustainable development of Africa from the UK. I will be in london throughout that day, will be a good idea to meet you then as i will be free from about 5pm in the evening.
Concerning the Java download, i am beggining to download it all over again. I bought a new mouse and its working(my touch pad doesnt work!) with my laptop now. will download the java compile r with the netbeans IDE. can u give me the proper link as the one i was downloading was giving me application server.
|
|
|
|
|
|
|
|
skima (m)
|
is this tutorial still on?
no1 seem to reply to any post or better stilll say about error in code or anything I have had add time solving the assignment but did i little thing but not to my satisfaction sha. while still trying to finalize it i think i should gv a little of what am battling with.
|
|
|
|
|
|