Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,414 members, 7,808,476 topics. Date: Thursday, 25 April 2024 at 12:32 PM

Java Programming For Dummies - Programming (3) - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Programming For Dummies (40459 Views)

Programming For Dummies. / Vb.net For Dummies: / Vb.net For Dummies: (2) (3) (4)

(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (Reply) (Go Down)

Re: Java Programming For Dummies by sbucareer(f): 12:31am On Feb 23, 2006


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.

Re: Java Programming For Dummies by sbucareer(f): 1:00am On Feb 23, 2006
[size=24pt]Comments in Java Code [/size]


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 nairaland Inc,
* ("Confidential Information"wink.  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

Re: Java Programming For Dummies by sbucareer(f): 1:22am On Feb 23, 2006
[size=24pt]Explaining the meaning of public static void main (String[] args)
[/size]


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.

Re: Java Programming For Dummies by sbucareer(f): 1:43am On Feb 23, 2006


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.



Re: Java Programming For Dummies by sbucareer(f): 1:58am On Feb 23, 2006


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.

Re: Java Programming For Dummies by sbucareer(f): 2:18am On Feb 23, 2006
[size=24pt]Code Snippets[/size]

[size=13pt]

/*
* @(#)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 nairaland Inc,
* ("Confidential Information"wink.  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.
[/size]
Re: Java Programming For Dummies by sbucareer(f): 2:43am On Feb 23, 2006


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.

Re: Java Programming For Dummies by skima(m): 6:41pm On Feb 23, 2006
@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
Re: Java Programming For Dummies by skima(m): 7:08pm On Feb 23, 2006
@sbucareer u have not replied to my mails. wats the latest abt my requests.
Re: Java Programming For Dummies by sbucareer(f): 7:38pm On Feb 23, 2006

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

Re: Java Programming For Dummies by sbucareer(f): 8:42pm On Feb 23, 2006

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.

[glow=red,0,5]Remember[/glow]
"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

Re: Java Programming For Dummies by sbucareer(f): 11:05pm On Feb 23, 2006



/*
* @(#)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 nairaland Inc,
* ("Confidential Information"wink.  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.
Re: Java Programming For Dummies by sbucareer(f): 11:54pm On Feb 23, 2006


Operator Use Description

[table]
[tr]
[td]+[/td][td] op1 + op2[/td][td]Adds op1 and op2; also used to concatenate strings[/td]
[/tr]
[tr]
[td]*[/td][td]op1 * op2[/td][td] Multiplies op1 by op2[/td]
[/tr]
[tr]
[td]/[/td][td]op1 / op2[/td][td] Divides op1 by op2[/td]
[/tr]
[tr]
[td]%[/td][td]op1 % op2[/td][td] Computes the remainder of dividing op1 by op2[/td]
[/tr]
[/table]


-    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




Re: Java Programming For Dummies by sbucareer(f): 12:50am On Feb 24, 2006

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.

Re: Java Programming For Dummies by sbucareer(f): 1:04am On Feb 24, 2006


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

Re: Java Programming For Dummies by Viper(m): 12:29pm On Feb 24, 2006
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. smiley

anyway, when u come online try and come on msn chat so we go talk as i am free today.
Re: Java Programming For Dummies by gbengaijot(m): 3:40pm On Feb 24, 2006
Can sun java studio creator be used for this tutorial?,
Re: Java Programming For Dummies by gbengaijot(m): 3:41pm On Feb 24, 2006
,
Re: Java Programming For Dummies by Seun(m): 3:50pm On Feb 24, 2006
Hello gbenga, didn't we download the Java+Netbeans stuff together? (sorry for interrupting)
Re: Java Programming For Dummies by gbengaijot(m): 4:28pm On Feb 24, 2006
Seun, we did, i installed it and the next thing is the applicastion server software that is in my root directory
Re: Java Programming For Dummies by Seun(m): 4:37pm On Feb 24, 2006
Check your Start Menu. Java/Netbeans should be there. Unless you installed a different one from the link I gave you.
Re: Java Programming For Dummies by gbengaijot(m): 4:52pm On Feb 24, 2006
.
Re: Java Programming For Dummies by sbucareer(f): 10:59pm On Feb 24, 2006

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.

Re: Java Programming For Dummies by skima(m): 9:31pm On Feb 26, 2006
@sbu u havent replied to my mail on our last chat. which i sent to ur gmail account .
Re: Java Programming For Dummies by Whitelady1(f): 1:54pm On Feb 27, 2006
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!
Re: Java Programming For Dummies by sbucareer(f): 2:03pm On Feb 27, 2006

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? [email=valentine.obih@gmail.com?subject=Last week assignment]PM[/email] if you have encountered any problem(s) and I would like to see your assignment for this week

Regards,

Valentine

Re: Java Programming For Dummies by sbucareer(f): 2:12pm On Feb 27, 2006

@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.

Re: Java Programming For Dummies by Whitelady1(f): 4:33pm On Feb 27, 2006
@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.
Re: Java Programming For Dummies by skima(m): 7:25pm On Feb 27, 2006
i was also talking about the software/book u said u will send me. U aint saying anything about it.


As to the assignment, am workin on it. i will submitt it late tuesday.
Re: Java Programming For Dummies by gbengaijot(m): 9:53pm On Mar 01, 2006
m
Re: Java Programming For Dummies by skima(m): 4:54pm On Mar 02, 2006
@gbenga here is the link

after the link just click accept then the real download page will load
link to whole down load
http://java.sun.com/j2se/1.5.0/download.jsp

link to jdk+netbean
http://java.sun.com/j2se/1.5.0/download-netbeans-50.html

link to jdk only

http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=jdk-1.5.0_06-oth-JPR&SiteId=JSC&TransactionId=noreg
Re: Java Programming For Dummies by skima(m): 6:01pm On Mar 03, 2006
is this tutorial still on?

no1 seem to reply to any post or better stilll say abt error in code or anything
I have had add time solving the assignment but did i lil thing but not to my satisfaction sha. while still trying to finalize it i think i should gv a lil of wat am battling with.

(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (Reply)

Nigerian Software Engineer given test to prove he is an engineer at JFK Airport / Facebook Is Suing Me For This / Learning To Program With Java by the Fulaman

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 68
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.