₦airaland Forum

Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,317,735 members, 5,108,910 topics. Date: Wednesday, 21 August 2019 at 11:28 PM

Learning To Program With Java by the Fulaman - Programming (4) - Nairaland

Nairaland Forum / Science/Technology / Programming / Learning To Program With Java by the Fulaman (29782 Views)

How To Program Arduino Uno / How To Program With Your Android Phone Using Aide IDE Environment / Which Training Center Can Someone Learn How To Program Quickly (2) (3) (4)

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

Re: Learning To Program With Java by the Fulaman by macaranta(m): 10:43am On Apr 13, 2016
Fulaman198:
I asked because I realised a flaw in my teaching today, since I was in such a hurry to put up some content (I should say yesterday really). I never mentioned Scanner methods nextDouble(), next(), etc. etc. just mentioned nextInt and nextLine. I'll pay closer attention to the next tutorial I put up. Thanks!

Hi,sent you a pm
Re: Learning To Program With Java by the Fulaman by satmaniac(m): 3:50am On Apr 14, 2016
Fulaman198, I hope all is well at your end. It's just that this silence is so uncomfortable. We hope to hearing from you sir.

1 Like

Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 4:09pm On Apr 14, 2016
satmaniac:
Fulaman198, I hope all is well at your end. It's just that
this silence is so uncomfortable. We hope to hearing from you sir.

Yes, I'll update this thread again soon. Don't worry.
Re: Learning To Program With Java by the Fulaman by satmaniac(m): 5:22pm On Apr 14, 2016
Fulaman198:

Yes, I'll update this thread again soon. Don't worry.
It is nice hearing from you again. *Heave a sigh of relief* Keeps ears to the ground.

1 Like

Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 11:47pm On Apr 14, 2016
Day 4:

It is so late and you heroes and heroines have been waiting a few days for it, so voila, here it is Day 4 tutorial on Java. Today, we will be discussing conditional statements which will continue into tomorrows lesson (that is if I can post it, you guys won't believe how incredibly busy I am with my own projects, thankfully I'm not married LOL). So enough joking around. Let's get started! Haba!

In many programming languages (especially with high-level languages like Java, C++, C#, Swift, etc), there are many ways to run tests on whether something is true or not. Often, the computer needs to decide at run-time what decision it will make before it can execute commands. Let us say for instance You have an array of Students (we will get to arrays later on) in which you would like to determine which what range of score is A, B, C, and then failing. If we want to have a range of values that determines letter grade A (for instance 70 - 100), or B ( 60 - 69), C (50 - 59), etc. how would we determine that.

Assume we have student X and we want to decide his final grade and print out a result to output? How would we do that? Well, that can be done with an If-Else-If-Else conditional structure. Here is the syntax for that:

if (statement is true) {

//then execute commands

} else-if (another statement is true) {

//then execute these commands


} else {

//execute these commands

}



What I have demonstrated is the syntax for an if-else block. Now, I will demonstrate it for you in actual code by writing a simple programme. However, before we write the programme, it's important that you the student understands the value of some operators:

In java, the following are important:

<= is less than or equal to
>= is greater than or equal to
== is equal to when testing for equality between primitive values
!= is not equal to
< stands for less than
> stands for greater than

This is very important when using an if test statement block.
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 11:48pm On Apr 14, 2016
In this simple programme, I write code to test for possible scenarios for a student's score based on Nigeria's grading system. Have a look at the code and the output which we will discuss.

Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 11:51pm On Apr 14, 2016
If you assess the posted source code, you can see that there are a series of if, else if, else scenarios. I give the ranges of A, B, C, and D. The user is prompted to input a student score at the beginning of the programme. The programme outpus how the student performed based on the numerical value.
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 12:05am On Apr 15, 2016
Alternatively, there is another way to write this programme. The alternative method to writing it is with a switch statement. The switch statement is another way to test conditional statements. The Switch statement works with byte, short, char and int primitive types. it also works with enum types which we will get to later, the String class and other classes capable of wrapping primitive types.

Let's say for instance that I would like to check the number of the month and correlate it to the proper month, how would I do that with a Switch statement? Very simply put like so:


package com.fulamannairalandtutorials;
import java.util.Scanner; //import a scanner object

public class SwitchUsage {
public static void main(String[] args) {
Scanner stdio = new Scanner(System.in );
int theMonth;

System.out.println("Please enter a numerical value for month" );
theMonth = stdio.nextInt();

switch(theMonth) {
case 1: System.out.println("The Month is January." );
break;

case 2: System.out.println("The Month is February." );
break;

case 3: System.out.println("The Month is March." );
break;

case 4: System.out.println("The Month is April." );
break;

case 5: System.out.println("The Month is May." );
break;

case 6: System.out.println("The Month is June." );
break;

case 7: System.out.println("The Month is July." );
break;

case 8: System.out.println("The Month is August." );
break;

case 9: System.out.println("The Month is Septembre." );
break;

case 10: System.out.println("The Month is Octobre." );
break;

case 11: System.out.println("The Month is Novembre." );
break;

case 12: System.out.println("The Month is Decembre." );
break;

default: System.out.println("You did not enter a valid number" );
break;
}

}
}

Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 12:10am On Apr 15, 2016
As one can see from the programme above, if you enter a valid numerical value from 1 - 12, it returns a String literal with the output that corresponds to the correct month.

In the second screenshot posted below the code, it shows what happens if you enter an integer value that is not in the range of 1 - 12. Entering a floating-point value will result in a runtime error because we have not learned how to deal with handling exceptions yet (that will come in a later tutorial).
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 12:20am On Apr 15, 2016
Day 4 Assignment:

Read the following documentation in order to supplement what you have learned today from this tutorial:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Your assignments are as follows:

1. Write a Programme that takes the grade programme that was posted earlier today and transform it using the Switch statement. The resulting output should be the same as with the if, else if and else statements

2. Write a programme that asks the user to enter the weight of a vehicle in kg. 2.2 lbs = 1 kg in case you don't know. Based on the table below, write your programme. You can use either a Switch statement or an if statement. That's up to you, but personally for this problem I would use a Switch statement.

Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 12:23am On Apr 15, 2016
I made an edit to the original post, this post now has operators and their respective functions and what they do. Enjoy and good night!
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 1:59am On Apr 15, 2016
macaranta:


Hi,sent you a pm

Hi, I didn't receive it, please send it again.
Re: Learning To Program With Java by the Fulaman by macaranta(m): 6:41am On Apr 15, 2016
Fulaman198:


Hi, I didn't receive it, please send it again.

OK re-sent
Re: Learning To Program With Java by the Fulaman by satmaniac(m): 7:39am On Apr 15, 2016
Fulaman198:
I made an edit to the original post, this post now has operators and their respective functions and what they do. Enjoy and good night!

Goodnight, and have a wonderful night. This switch and and if-else-if-else statements is quite a powerful stuff. I will have to read day4 tutorial again to make sure I have a good understanding of the topic at hand.

Thank you for taking time out of your busy schedule to see your students.

1 Like

Re: Learning To Program With Java by the Fulaman by Nobody: 8:04am On Apr 15, 2016
Fulaman198:
Day 4 Assignment:
Read the following documentation in order to...
Your assignments are as follows:
1. Write a Programme tha....
Plz can you help me upload the image as a downloadable attachment...
Re: Learning To Program With Java by the Fulaman by Dekatron(m): 10:22am On Apr 15, 2016
Thanks for the tuts. . . I have some stuffs to ask
Re: Learning To Program With Java by the Fulaman by satmaniac(m): 10:51am On Apr 15, 2016
Fulaman198:
Alternatively, there is another way to write this programme. The alternative method to writing it is with a switch statement. The switch statement is another way to test conditional statements. The Switch statement works with byte, short, char and int primitive types. it also works with enum types which we will get to later, the String class and other classes capable of wrapping primitive types.

Let's say for instance that I would like to check the number of the month and correlate it to the proper month, how would I do that with a Switch statement? Very simply put like so:


package com.fulamannairalandtutorials;
import java.util.Scanner; //import a scanner object

public class SwitchUsage {
public static void main(String[] args) {
Scanner stdio = new Scanner(System.in );
int theMonth;

System.out.println("Please enter a numerical value for month" );
theMonth = stdio.nextInt();

switch(theMonth) {
case 1: System.out.println("The Month is January." );
break;

case 2: System.out.println("The Month is February." );
break;

case 3: System.out.println("The Month is March." );
break;

case 4: System.out.println("The Month is April." );
break;

case 5: System.out.println("The Month is May." );
break;

case 6: System.out.println("The Month is June." );
break;

case 7: System.out.println("The Month is July." );
break;

case 8: System.out.println("The Month is August." );
break;

case 9: System.out.println("The Month is Septembre." );
break;

case 10: System.out.println("The Month is Octobre." );
break;

case 11: System.out.println("The Month is Novembre." );
break;

case 12: System.out.println("The Month is Decembre." );
break;

default: System.out.println("You did not enter a valid number" );
break;
}

}
}


Care to explain the line of code in blue, up above?
Re: Learning To Program With Java by the Fulaman by asalimpo(m): 11:50am On Apr 15, 2016
satmaniac:


Care to explain the line of code in blue, up above?
if the op doesnt mind or his student ...

That statement creates a package - a group of related classes grouped together as a unit.
the package can be imported for use in other programs , facilitating code-reuse and avoiding name collision conflicts.
e.g if you have a class with the same name as a native class, you can differentiate yours and avoid confusing the compiler by placing yours in a package.

When compiled, a folder is created in the order of the package name. subfolders appear according to their
dot separation in the package name.
e.g
for the class SwitchUsage
in package
com.fullermannairalandtutorial
the following folder structure will be created com/fullermannairalandtutorials
with the class SwitchUsage in the last folder , fullermannairalandtutorials .


to use this class in other programs, you use the import statement.

like this:
import com.fullermantutorials.SwitchUsage -

if you are importing more classes but dont want to specify every class by name u , use the wildcard character:
e.g
you'll often see codes like this:
import java.util.*
That statement imports all the classes in the java.util package.

by convention , package names are specified in lowercase letters.
classes, in uppercase.
You can't create a package that begins with java - that name is reserved to avoid confusion and clashes with the pre-built java packages.

1 Like 1 Share

Re: Learning To Program With Java by the Fulaman by satmaniac(m): 12:41pm On Apr 15, 2016
Fulaman198:
Day 4 Assignment:

Read the following documentation in order to supplement what you have learned today from this tutorial:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Your assignments are as follows:

1. Write a Programme that takes the grade programme that was posted earlier today and transform it using the Switch statement. The resulting output should be the same as with the if, else if and else statements

2. Write a programme that asks the user to enter the weight of a vehicle in kg. 2.2 lbs = 1 kg in case you don't know. Based on the table below, write your programme. You can use either a Switch statement or an if statement. That's up to you, but personally for this problem I would use a Switch statement.

This one strong o! See as I just dey sweat on top the assignment and still I am not making any headway. I have been having this illegal-start-of-an-expression error on my compiler. Hmm could it be that our teacher is trying to drive home some points about the difference in using the switch and the if-else-ifelse? Who don succeed make e come give me expo na. Or are we allowed to used google to helep solve this issue, our teacher Fulaman198?

1 Like

Re: Learning To Program With Java by the Fulaman by satmaniac(m): 12:43pm On Apr 15, 2016
asalimpo:

if the op doesnt mind or his student ...

That statement creates a package - a group of related classes grouped together as a unit.
the package can be imported for use in other programs , facilitating code-reuse and avoiding name collision conflicts.
e.g if you have a class with the same name as a native class, you can differentiate yours and avoid confusing the compiler by placing yours in a package.

When compiled, a folder is created in the order of the package name. subfolders appear according to their
dot separation in the package name.
e.g
for the class SwitchUsage
in package
com.fullermannairalandtutorial
the following folder structure will be created com/fullermannairalandtutorials
with the class SwitchUsage in the last folder , fullermannairalandtutorials .


to use this class in other programs, you use the import statement.

like this:
import com.fullermantutorials.SwitchUsage -

if you are importing more classes but dont want to specify every class by name u , use the wildcard character:
e.g
you'll often see codes like this:
import java.util.*
That statement imports all the classes in the java.util package.

by convention , package names are specified in lowercase letters.
classes, in uppercase.
You can't create a package that begins with java - that name is reserved to avoid confusion and clashes with the pre-built java packages.

Just like the vigilante in the seasonal film "Arrow" you always come to the rescue. Thanks Oga asalimpo.
Re: Learning To Program With Java by the Fulaman by Damoxy(m): 12:48pm On Apr 15, 2016
pls any link to download andriod studio tutorials for free for begineers and intermediates
Re: Learning To Program With Java by the Fulaman by Dekatron(m): 3:55pm On Apr 15, 2016
@Asalimpo and Fulaman198, how do I print a value together with another. I mean :- look at python :-


print('1\b2\b3')
would print :-

123 TOGETHER
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 4:23pm On Apr 15, 2016
crotonite:


Plz can you help me upload the image as a downloadable attachment...

Not sure what you want from me here bro.
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 4:26pm On Apr 15, 2016
asalimpo:

if the op doesnt mind or his student ...

That statement creates a package - a group of related classes grouped together as a unit.
the package can be imported for use in other programs , facilitating code-reuse and avoiding name collision conflicts.
e.g if you have a class with the same name as a native class, you can differentiate yours and avoid confusing the compiler by placing yours in a package.

When compiled, a folder is created in the order of the package name. subfolders appear according to their
dot separation in the package name.
e.g
for the class SwitchUsage
in package
com.fullermannairalandtutorial
the following folder structure will be created com/fullermannairalandtutorials
with the class SwitchUsage in the last folder , fullermannairalandtutorials .


to use this class in other programs, you use the import statement.

like this:
import com.fullermantutorials.SwitchUsage -

if you are importing more classes but dont want to specify every class by name u , use the wildcard character:
e.g
you'll often see codes like this:
import java.util.*
That statement imports all the classes in the java.util package.

by convention , package names are specified in lowercase letters.
classes, in uppercase.
You can't create a package that begins with java - that name is reserved to avoid confusion and clashes with the pre-built java packages.

Good explanation and to answer your question, I don't mind. I don't mind others chiming in from time to time. It saves me time and the effort smiley
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 4:26pm On Apr 15, 2016
satmaniac:


This one strong o! See as I just dey sweat on top the assignment and still I am not making any headway. I have been having this illegal-start-of-an-expression error on my compiler. Hmm could it be that our teacher is trying to drive home some points about the difference in using the switch and the if-else-ifelse? Who don succeed make e come give me expo na. Or are we allowed to used google to helep solve this issue, our teacher Fulaman198?


You may use other resources if it helps you understand the material better.
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 4:40pm On Apr 15, 2016
Dekatron:
@Asalimpo and Fulaman198, how do I print a value together with another. I mean :- look at python :-


print('1\b2\b3')
would print :-

123 TOGETHER

I thought that this was discussed earlier, but if it was not, I'm sorry it should have been, it is called string concatenation. To join strings together you simply use the '+' symbol.

E.g.



String myString1 = "This is " ;
String myString2 = "Java " ;
String myString3 = "Programming." ;

System.out.println(myString1 + myString2 + myString3 );


I hope that helps.
Re: Learning To Program With Java by the Fulaman by Fulaman198(m): 4:42pm On Apr 15, 2016
Damoxy:
pls any link to download andriod studio tutorials for free for begineers and intermediates

http://www.tutorialspoint.com/android/index.htm
Re: Learning To Program With Java by the Fulaman by Nobody: 5:12pm On Apr 15, 2016
Fulaman198:


Not sure what you want from me here bro.
the attached image is not clear on my mobile device. what i want is that you rename the image to something like: 'image_name.jpg.zip' b4 uploading so that i can dowload it instead. tnx
Re: Learning To Program With Java by the Fulaman by Dekatron(m): 5:16pm On Apr 15, 2016
Fulaman198:


I thought that this was discussed earlier, but if it was not, I'm sorry it should have been, it is called screen concatenation. To join strings together you simply use the '+' symbol.

E.g.



String myString1 = "This is " ;
String myString2 = "Java " ;
String myString3 = "Programming." ;

System.out.println(myString1 + myString2 + myString3 );


I hope that helps.
Re: Learning To Program With Java by the Fulaman by Dekatron(m): 5:17pm On Apr 15, 2016
Fulaman198:


I thought that this was discussed earlier, but if it was not, I'm sorry it should have been, it is called screen concatenation. To join strings together you simply use the '+' symbol.

E.g.



String myString1 = "This is " ;
String myString2 = "Java " ;
String myString3 = "Programming." ;

System.out.println(myString1 + myString2 + myString3 );


I hope that helps.


yea. . . I know concatenation, but that wasn't working . . Rectified it myself though. . . I used
System.out.print(myWhatever was here);





Actually, I just finally finished my resistor calculator program RIGHT NOW. . . After labouring for WEEKS. . . I got it under an hour. grin grin . . . I'd attach Screenshots when I have data. . . . .





Now, all you need to do is input the color band on a resistor, and you'd get the value in OHMS!! grin grin am so GAY!! Lol. . . I feel fufilled . . . Thanks to you guys and to . . Thinking and thinking (even in Engineering courses classes)



trying to import into ANDROID studio/ Trying to develop from Eclipse directly too. . . . Thanks bro

1 Like

Re: Learning To Program With Java by the Fulaman by asalimpo(m): 7:10pm On Apr 15, 2016
Fulaman198:


I thought that this was discussed earlier, but if it was not, I'm sorry it should have been, it is called screen concatenation. To join strings together you simply use the '+' symbol.

E.g.



String myString1 = "This is " ;
String myString2 = "Java " ;
String myString3 = "Programming." ;

System.out.println(myString1 + myString2 + myString3 );


I hope that helps.
screen string concatenation.
Re: Learning To Program With Java by the Fulaman by asalimpo(m): 7:12pm On Apr 15, 2016
Fulaman198:


Good explanation and to answer your question, I don't mind. I don't mind others chiming in from time to time. It saves me time and the effort smiley
No, problemo . We are with you.

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

Nigerian Teen Girls Win International Award With Their Waste Disposal App / A Thread For Tutorial On Python Programming / Ethical Hackers

Viewing this topic: 1 guest(s)

(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: (0) (1) (2) (3) (4) (5) (6) (7) (8) (9)

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