Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,206,474 members, 7,995,849 topics. Date: Wednesday, 06 November 2024 at 04:48 PM

Learn Java Programming Here Easily - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Learn Java Programming Here Easily (17429 Views)

Learn Programming Here / Best School To Learn Java In Lagos (2) (3) (4)

(1) (2) (3) (Go Down)

Re: Learn Java Programming Here Easily by azpunpin: 9:02pm On Mar 10, 2009
Bossman, it still brings out an error, I set my path to my new JDK Version. I think that is not a problem.

Addition.java:1: cannot resolve symbol
symbol : class Scanner
location: package util
import java.util.Scanner;
^
Addition.java:7: cannot resolve symbol
symbol : class Scanner
location: class Addition
Scanner kb = new Scanner(System.in);
^
Addition.java:7: cannot resolve symbol
symbol : class Scanner
location: class Addition
Scanner kb = new Scanner(System.in);
^
3 errors




One Love.
Re: Learn Java Programming Here Easily by Bossman(m): 9:46pm On Mar 10, 2009
That's what I thought. The compiler is not finding that class, which makes me believe your CLASSPATH is still pointing to the old JDK. Usually, it should have updated automatically.

You can either go to control panel and verify that your classpath is correct or type 'set classpath' at the command prompt and see what it says. 

azpunpin:

Bossman, it still brings out an error, I set my path to my new JDK Version. I think that is not a problem.

Addition.java:1: cannot resolve symbol
symbol : class Scanner
location: package util
import java.util.Scanner;
^
Addition.java:7: cannot resolve symbol
symbol : class Scanner
location: class Addition
Scanner kb = new Scanner(System.in);
^
Addition.java:7: cannot resolve symbol
symbol : class Scanner
location: class Addition
Scanner kb = new Scanner(System.in);
^
3 errors




One Love.
Re: Learn Java Programming Here Easily by azpunpin: 10:02pm On Mar 10, 2009
I think, av fixed it but am having this error now

C:\Projects\nairaland\src>javac Additional.java
Additional.java:12: cannot find symbol
symbol : variable nextInt
location: class java.util.Scanner
firstValue = kb.nextInt;
^
Additional.java:14: cannot find symbol
symbol : variable nextInt
location: class java.util.Scanner
secondValue = kb.nextInt;
^
2 errors



One Love
Re: Learn Java Programming Here Easily by azpunpin: 10:26pm On Mar 10, 2009
I HAVE ENTUALLY SOLVE THE ABOVE ALSO, NOW I HAVE THIS ERROR AFTER javac Additional.java WHEN TRYING TO java Additional. It brings this error:

C:\Projects\nairaland\src>java Additional
Exception in thread "main" java.lang.NoClassDefFoundError: Additional
Caused by: java.lang.ClassNotFoundException: Additional
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Additional. Program will exit.



One Love.
Re: Learn Java Programming Here Easily by ade2kay(m): 8:28am On Mar 11, 2009
azpunpin:

firstValue = kb.nextInt;


The above is wrong.

You are trying to invoke the nextInt method on kb (an instance of the Scanner class), and not access a public member variable of the instance

It should be kb.nextInt();
Re: Learn Java Programming Here Easily by ade2kay(m): 8:33am On Mar 11, 2009
azpunpin:

I HAVE ENTUALLY SOLVE THE ABOVE ALSO, NOW I HAVE THIS ERROR AFTER javac Additional.java WHEN TRYING TO java Additional. It brings this error:

C:\Projects\nairaland\src>java Additional
Exception in thread "main" java.lang.NoClassDefFoundError: Additional
Caused by: java.lang.ClassNotFoundException: Additional
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Additional. Program will exit.



One Love.

and for this error, the problem could be as a result of any of the following:

1. Either the code has been compiled yet, so the java classloader can't load the class
2. If the class is defined as part of a package, to run this program, you use java packagestructure.classname and not java classname

So, fix any of the above that applies to you

cool
Re: Learn Java Programming Here Easily by okunade(m): 1:59pm On Mar 11, 2009
azpunpin:

Bossman, it still brings out an error, I set my path to my new JDK Version. I think that is not a problem.

Addition.java:1: cannot resolve symbol
symbol : class Scanner
location: package util
import java.util.Scanner;
^
Addition.java:7: cannot resolve symbol
symbol : class Scanner
location: class Addition
Scanner kb = new Scanner(System.in);
^
Addition.java:7: cannot resolve symbol
symbol : class Scanner
location: class Addition
Scanner kb = new Scanner(System.in);
^
3 errors




One Love.


pls visit www.java.sun.com for your jdk update
Re: Learn Java Programming Here Easily by azpunpin: 2:13pm On Mar 11, 2009
I couldn`t be able to fix it but i am now using java -classpath . filname to execute and it works.

Okunade, when will u continue the lecture. Hope to learn more n more day by day! I Luv Java!


One Love
Re: Learn Java Programming Here Easily by okunade(m): 3:47pm On Mar 11, 2009
LESSON ONE,

Program Skeleton:
Enter the following program skeleton, compile (prepare it to run), and then run (execute).

public class Tester
{
 public static void main(String args[])
{
 
}
}

At this point don’t worry about what any of this means. It’s just something we must do
every time. Soon we will learn the meaning of all of this. For now it’s just the skeleton
that we need for a program.

Adding some meaningful code:
Now, let’s add some meaningful code inside the main method. (Notice this word,
METHOD. We will constantly refer to methods throughout this TUTORIAL.) We will also add a
remark.

public class Tester //We could put any name here besides Tester
{
 public static void main(String args[])
{
 System.out.println("Hello world"wink;
}
}


Remarks:
Notice the rem (remark or in Fortran or C++ call comment) above that starts with //. You can put remarks anywhere in the program without it affecting program operation. Remarks are also called comments or
notes.

Printing:

System.out.println("Hello world" );
This is how we get the computer to printout something.
Notice the trailing semicolon. Most lines of code are required to end in a semicolon.

Now try putting in some other things in the println parenthesis above. Each time
recompile and run the program:

1.  "Peter Piper picked a peck of pickled peppers."
2.  "I like computer science."
3.  25/5
4.  4 / 7.0445902
5.  13 * 159.56

Two printlns for the price of one:

Next, modify your program so that the main method looks as follows:

 public static void main(String args[])
{
 System.out.println(“Hello world”);
 System.out.println(“Hello again”);  
}


Run this and note that it prints :
 Hello world
 Hello again


Printing “Sideways”:
Now remove the ln from the first println as follows:

public static void main(String args[])
{
 System.out.print(“Hello world”);
 System.out.println(“Hello again”);  
}


Run this and note that it prints:
Hello worldHello again
 
Here are the rules concerning println and print:

•  System.out.println( ) completes printing on the current line and pulls the print
position down to the next line where any subsequent printing continues.
•  System.out.print( ) prints on the current line and stops there. Any subsequent
printing continues from that point.

An in-depth look at rems:
Let’s take a further look at rems. Consider the following program (class) in which we
wish to document ourselves as the programmer, the date of creation, and our school:

public class Tester
{
 //Programmer: Okunade
 //Date created: Mar 11, 2009
 //School:  OAU

 public static void main(String args[])
{
 System.out.println("Hello again"wink;  
}
}


Block rems:
It can get a little tedious putting the double slash rem-indicator in front of each
line, especially if we have quite a few remark lines. In this case we can “block
rem” all the comment lines as follows:

public class Tester
{
 /*Programmer: Okunade
 Date created: Mar 11, 2009
 School: OAU

 public static void main(String args[])
{
 System.out.println("Hello again"wink;  
}
}


Notice we use  /*  to indicate the start of the block and  */  for the end.
Everything between these two symbols is considered to be a remark and will be
ignored by the computer when compiling and running.

HERE IS ASSIGNMENT FOR YOU,

Project… From Me To You
Create a new project called FromMeToYou having a Tester class with the following content.
Also include remarks above public class Tester that identifies you as the author along with the
date of creation of this program:

 //Author: Okunade
//Date created: Mar 22, 2009
public class Tester
{
   public static void main(String args[])
   {
       ------------------------------
   }
}

Supply code in the place of  --------------- that will produce the following printout:

From: Okunade
Address: OAU Computer, Bldg Ife
Date: Mar 11, 2009

To: Bill Gates

Message: Help! I need to be a Programmer!

Re: Learn Java Programming Here Easily by azpunpin: 6:28pm On Mar 11, 2009
okunade:

LESSON ONE,

Program Skeleton:
Enter the following program skeleton, compile (prepare it to run), and then run (execute).

public class Tester
{
public static void main(String args[])
{

}
}

At this point don’t worry about what any of this means. It’s just something we must do
every time. Soon we will learn the meaning of all of this. For now it’s just the skeleton
that we need for a program.

Adding some meaningful code:
Now, let’s add some meaningful code inside the main method. (Notice this word,
METHOD. We will constantly refer to methods throughout this TUTORIAL.) We will also add a
remark.

public class Tester //We could put any name here besides Tester
{
public static void main(String args[])
{
System.out.println("Hello world"wink;
}
}


Remarks:
Notice the rem (remark or in Fortran or C++ call comment) above that starts with //. You can put remarks anywhere in the program without it affecting program operation. Remarks are also called comments or
notes.

Printing:

System.out.println("Hello world" );
This is how we get the computer to printout something.
Notice the trailing semicolon. Most lines of code are required to end in a semicolon.

Now try putting in some other things in the println parenthesis above. Each time
recompile and run the program:

1. "Peter Piper picked a peck of pickled peppers."
2. "I like computer science."
3. 25/5
4. 4 / 7.0445902
5. 13 * 159.56

Two printlns for the price of one:

Next, modify your program so that the main method looks as follows:

public static void main(String args[])
{
System.out.println(“Hello world”);
System.out.println(“Hello again”);
}


Run this and note that it prints :
Hello world
Hello again


Printing “Sideways”:
Now remove the ln from the first println as follows:

public static void main(String args[])
{
System.out.print(“Hello world”);
System.out.println(“Hello again”);
}


Run this and note that it prints:
Hello worldHello again

Here are the rules concerning println and print:

• System.out.println( ) completes printing on the current line and pulls the print
position down to the next line where any subsequent printing continues.
• System.out.print( ) prints on the current line and stops there. Any subsequent
printing continues from that point.

An in-depth look at rems:
Let’s take a further look at rems. Consider the following program (class) in which we
wish to document ourselves as the programmer, the date of creation, and our school:

public class Tester
{
//Programmer: Okunade
//Date created: Mar 11, 2009
//School: OAU

public static void main(String args[])
{
System.out.println("Hello again"wink;
}
}


Block rems:
It can get a little tedious putting the double slash rem-indicator in front of each
line, especially if we have quite a few remark lines. In this case we can “block
rem” all the comment lines as follows:

public class Tester
{
/*Programmer: Okunade
Date created: Mar 11, 2009
School: OAU

public static void main(String args[])
{
System.out.println("Hello again"wink;
}
}


Notice we use /* to indicate the start of the block and */ for the end.
Everything between these two symbols is considered to be a remark and will be
ignored by the computer when compiling and running.

HERE IS ASSIGNMENT FOR YOU,

Project… From Me To You
Create a new project called FromMeToYou having a Tester class with the following content.
Also include remarks above public class Tester that identifies you as the author along with the
date of creation of this program:

//Author: Okunade
//Date created: Mar 22, 2009
public class Tester
{
public static void main(String args[])
{
------------------------------
}
}

Supply code in the place of --------------- that will produce the following printout:

From: Okunade
Address: OAU Computer, Bldg Ife
Date: Mar 11, 2009

To: Bill Gates

Message: Help! I need to be a Programmer!




BELOW IS THE ASSIGNMENT:


public class Tester
{
//Programmer: Azpunpin
//Date created: Mar 11, 2009
//School: Lead City University
//Time: 6:20PM

public static void main(String args[])
{
System.out.println("From: Azpunpin\nAddress: LEAD CITY UNIVERSITY\nDate: Mar 11, 2009\nTo: Bill Gates\nMessage: Help! I need to be a Programmer!"wink;
}
}
Re: Learn Java Programming Here Easily by okunade(m): 9:18pm On Mar 11, 2009

LESSON TWO,

Variable Types (String, int, double)

Three variable types:

(A good way to learn the following points is to modify the code of the "Hello World"
program according to the suggestions below.)

1. String….used to store things in quotes….like "Hello world"
Sample code:

[color=#000099] public static void main(String args[])
{
String s = "Hello cruel world";
System.out.println(s);
}


2. int ….used to store integers (positive or negative)
Sample code:

public static void main(String args[])
{
int age = 59;
System.out.println(age);
}


3. double ….used to store "floating point" numbers (decimal fractions). double means
"double precision".

Sample code:

public static void main(String args[])
{
double d = -137.8036;
System.out.println(d);

d = 1.45667E23; //Scientific notation…means 1.45667 X 10+23
}


Declaring and initializing:
When we say something like

double x = 1.6;

we are really doing two things at once. We are declaring x to be of type double and we
are initializing x to the value of 1.6. All this can also be done in two lines of code (as
shown below) instead of one if desired:

double x; //this declares x to be of type double
x = 1.6; //this initializes x to a value of 1.6

What’s legal and what’s not:

int arws = 47.4; //illegal, won’t compile since a decimal number cannot “fit” into an integer variable.
double d = 103; //legal…same as saying the decimal number 103.0



Rules for variable names:

1. Variable names must begin with a letter (or an underscore character) and cannot contain spaces.
2. The only “punctuation” character permissible inside the name is the underscore (“_”).
3. Variable names cannot be one of the reserved words (key words…)that are part of the Java language.

Legal names Illegal names
Agro 139
D 139Abc
d31 fast One
hoppergee class
hopper_gee slow.Sally
largeArea double
goldNugget gold;Nugget
hopper-gee

Variable naming conventions:
It is traditional (although not a hard and fast rule) for variable names to start with a
lower case letter. If a variable name consists of multiple words, combine them in one of
two ways:

bigValue… jam everything together. First word begins with a small letter and
subsequent words begin with a capital.

big_value… separate words with an underscore.
-------------------------------------------------------------------------------------------------------------------------
EXERCISE ON LESSON TWO

1. What are the three main types of variables used in Java and what are they used to store?


2. What type of variable would you use to store your name?


3. What type of variable would you use to store the square root of 2?


4. What type of variable would you use to store your age?


5. Write a single line of code that will create a double precision variable called p and store
1.921 X 10-16 in it.


6. Write a single line of code that will create an integer variable called i and store 407 in
it.


7. Write a single line of code that will create a String variable called my_name and store
your name in it.


8. Write a line of code that will declare the variable count to be of type int. Don’t
initialize.


9. Write a line of code that initializes the double precision variable bankBalance to
136.05. Assume this variable has already been declared.


10. Which of the following are legal variable names?
scooter13 139_scooter homer-5 ;mary public doubled double ab c

11. Which of the following is the most acceptable way of naming a variable. Multiple
answers are possible.

a. GroovyDude
b. GROOVYDUDE
c. groovyDude
d. Groovydude
e. groovy_dude
f. groovydude

12. Comment on the legality of the following two lines of code.
double dist = 1003;
int alt = 1493.86;
[/color]
Re: Learn Java Programming Here Easily by jacob05(m): 10:50am On Mar 12, 2009
Infant questions grin tongue waiting for the Real Questions
Re: Learn Java Programming Here Easily by staroye: 10:19pm On Mar 12, 2009
Hi Mr. Okunade, am new on this site but I want to know if u Can try to teach me java and c++ after ur exam pls.
Re: Learn Java Programming Here Easily by okunade(m): 1:02am On Mar 13, 2009
hi mr staroye
follow the tutorial from lesson one, read it carefully, and do all the assignment.

hey JACOB05 THIS THREAD MAY NOT BE FOR YOU, BUT NOVICE IN JAVA OK
Re: Learn Java Programming Here Easily by azpunpin: 6:48am On Mar 13, 2009
SORRY I WAS BUSY WITH EXAMS, SO I COULDN`T LAY MY HANDS ON THE ASSIGNMENT FAST.




1. What are the three main types of variables used in Java and what are they used to store?

int, double and String


2. What type of variable would you use to store your name?

String


3. What type of variable would you use to store the square root of 2?

double


4. What type of variable would you use to store your age?

int


5. Write a single line of code that will create a double precision variable called p and store
1.921 X 10-16 in it.

public static void main(String args[])
{
double d = 13.21;
System.out.println(d);
}


6. Write a single line of code that will create an integer variable called i and store 407 in
it.

public static void(String args[])
{
int i = 407;
System.out.println(i);

}
}

7. Write a single line of code that will create a String variable called my_name and store
your name in it.

public static void(String args[])
{
String my_name = Azeez;
System.out.println(my_name);

}
}

8. Write a line of code that will declare the variable count to be of type int. Don’t
initialize.

I DON`T UNDERSTAND THIS QUESTION PROPERLY

9. Write a line of code that initializes the double precision variable bankBalance to
136.05. Assume this variable has already been declared.

public static void main(String args[])
{
double bankBalance = 136.05;
System.out.println(bankBalance);
}
}

10. Which of the following are legal variable names?
scooter13 139_scooter homer-5 ;mary public doubled double ab c

Answer: scooter13

11. Which of the following is the most acceptable way of naming a variable. Multiple
answers are possible.

a. GroovyDude
b. GROOVYDUDE
c. groovyDude
d. Groovydude
e. groovy_dude
f. groovydude

Answer: groovydude,groovy_dude,groovyDude

12. Comment on the legality of the following two lines of code.
double dist = 1003;
int alt = 1493.86;

Answer: double will store it, it will take it as 1003.0
Int won`t store it because it won`t store decimal values.
[/color]
Re: Learn Java Programming Here Easily by okunade(m): 8:37am On Mar 13, 2009
Im very happy for you azpunpin, if you continue like this you will be a great programmer. All the ans are correct, but except
question 8, 9, 10

note there is a different between declaring a variable and initializing a variable.

//Declaration
double pay; // I've only declared pay as type double

//Initialization
pay = 50.50; // pay was initialized to a double value 50.50

so number 8.
{
int count; // Answer, Don't initialize
}

number 9.
{
bankBalance = 136.05; // Assume it has already been declared.
}

number 10.

doubled & scooter13

Note: I will post all the java keyword that cannot be used as a variable name.

I will also post Lesson 3 tonight INSHA Allah
Re: Learn Java Programming Here Easily by azpunpin: 8:55am On Mar 13, 2009
Thanks Okunade. I will be waiting for more lessons, meanwhile am trying to read further with Java How to Program. But i need more n more lessons like you are doing.

Thank so much cheesy

One Love.
Re: Learn Java Programming Here Easily by okunade(m): 5:37pm On Mar 13, 2009
LESSON THREE

Simple String Operations  

In this lesson we will learn just a few of the things we can do with Strings.

1. Concatenation:

First and foremost is concatenation. We use the plus sign, +, to do this. For example:

String mm = "Hello";
String nx = "good buddy";
String c = mm + nx;
System.out.println(c);  //prints [color=#990000]Hellogood buddy
…notice no space between o & g
[/color]
The above code could also have been done in the following way:

String mm = "Hello";
String nx = "good buddy";
System.out.println(mm + "  " + nx);  //prints [color=#990000]Hello good buddy
…notice the space
[/color]
We could also do it this way:

System.out.println("Hello" + "  good buddy"wink;  // prints Hello good buddy


2. The length method:

Use the length( ) method to find the number of characters in a String: (NOTE the word METHOD*)

String theName = "Donald Duck";
int len = theName.length( );
System.out.println(len);  //prints 11…[color=#990000]notice the space gets counted

[/color]
 Right now we don’t see much value in this length thing…just wait!

A piece of a String (substring):
We can pick out a piece of a String…substring

String myPet = "Sparky the dog";
String smallPart = myPet.substring(4);
System.out.println(smallPart);  //prints [color=#990000]ky the dog
[/color]

Why do we get this result? The various characters in a String are numbered starting on the
left with 0. These numbers are called indices. (Notice the spaces are numbered too.)

 S p a  r k y    t h  e       d   o  g …    so now we see that the ‘k’ has index 4 and we go from
 0 1 2 3 4 5 6 7 8 9 10 11 12 13       k all the way to the end of the string to get “ky the dog”.

A more useful form of substring:
But wait! There’s another way to use substring

String myPet = "Sparky the dog";
String smallPart = myPet.substring(4, 12);
System.out.println(smallPart);  //prints [color=#990000]ky the d

[/color]
 How do we get ky the d? Start at k, the 4th index, as before. Go out to the 12th index, 'o' in
this case and pull back one notch. That means the last letter is d.


Conversion between lower and upper case:
1. toLowerCase converts all characters to lower case (small letters)

String bismark = "Dude, where’s MY car?";
System.out.println( bismark.toLowerCase( ) );  // prints dude, where’s my car?

2. toUpperCase converts all characters to upper case (capital letters)


System.out.println( "Dude, where’s My car?".toUpperCase( ) );
//prints DUDE, WHERE’S MY CAR?

Note: length, substring, toLowerCase, and toUpperCase are all methods of the String CLASS
There are other methods we will learn later.

 

Concatenating a String and a numeric:
It is possible to concatenate a String with a numeric variable as follows:

int x = 27;
String s = "What have we done?"
String combo = s + " " + x;
System.out.println(combo);  //prints What have we done? 27


Escape sequences:
How do we force a quote character (") to printout…. or, to be part of a String. Use the
escape sequence, \", to print the following (note escape sequences always start with the \
character… more on escape sequences later):

What "is" the right way?

String s = "What \"is\" the right way?";
System.out.println(s);  //prints What "is" the right way?


Another escape sequence,  \n,  will create a new line (also called line break) as shown
below:

String s = "Here is one line\nand here is another.";
System.out.println(s);

Prints the following:
 Here is one line
 and here is another.

The escape sequence, \\, will allow us to print a backslash within our String. Otherwise, if
we try to insert just a single \ it will be interpreted as the beginning of an escape sequence.

System.out.println("Path = c:\\Program_file.doc"wink;

Prints the following:

Path = c:\Program_file.doc

The escape sequence, \t, will allow us to “tab” over. The following code tabs twice.
System.out.println(“Name:\t\tAddress:”);

Prints the following:

Name:                 Address:
-----------------------------------------------------------------------------------------------------------------------------

EXERCISE ON LESSON THREE,

1.  Write code in which a String variable s contains “The number of rabbits is”. An integer
variable argh has a value of 129. Concatenate these variables into a String called report.
Then print report. The printout should yield:
The number of rabbits is 129.
*Note that we want a period to print after the 9.

2.  What is the output of     System.out.println( p.toUpperCase( ) );   if p = “Groovy Dude”?

3.  Write code that will assign the value of “Computer Science is for nerds” to the String
variable g. Then have it print this String with nothing but “small” letters.

4.  What will be the value of c?
String c;
String m = “The Gettysburg Address”;
c = m.substring(4);
   
5.  What will be the value c?
String b = “Four score and seven years ago,”;
c = b.substring(7, 12);
   
6.  What is the value of count?
int count;
String s = “Surface tension”;
   count = s.length( );  
   
7.  Write code that will look at the number of  characters in String m = “Look here!”; and then
print
“Look here!” has 10 characters.
*Note Use the length( ) method to print the 10 ….you must also force the two quotes to print.
 

8.  How would you print the following?
All “good” men should come to the aid of their country.  

 9.  Write code that will produce the following printout using only a single println( ).
Hello
Hello again

10. Write code that will produce the following printout.
A backslash looks like this \, …right?

11. What is output by the following?
String pq = “Eddie Haskel”;
int  hm = pq.length( );
String ed = pq.substring(hm - 4);
 System.out.println(ed);
   
12. Which character is at the 5th index in the String “Herman Munster”?




PROJECT, … Name that Celebrity

Create a new project called NameThatCelebrity in which only partially recognizable names of
celebrities are to be produced. In a real implementation of this game, the idea is for a contestant
to be able to guess the real name of the celebrity after the first two and last three letters are
dropped from the name. We have been given the task of testing the feasibility of this idea by
producing the following printout:

Allan Alda>>>lan A
John Wayne>>>hn Wa
Gregory Peck>>>egory P

Begin your code within the main method as follows:

 String s1 = “Allan Alda”;
 String s2 = “John Wayne”;
 String s3 = “Gregory Peck”;

Apply the length and substring methods to these Strings to produce the above printout.

Re: Learn Java Programming Here Easily by azpunpin: 7:47pm On Mar 13, 2009
SOLUTION TO THE EXERCISE

1. Write code in which a String variable s contains “The number of rabbits is”. An integer
variable argh has a value of 129. Concatenate these variables into a String called report.
Then print report. The printout should yield:
The number of rabbits is 129.
*Note that we want a period to print after the 9.

int f = 129;
String d = "The number of rabbits is?"
String report = d + " " + f;
System.out.println(report);

2. What is the output of System.out.println( p.toUpperCase( ) ); if p = “Groovy Dude”?

GROOVY DUDE

3. Write code that will assign the value of “Computer Science is for nerds” to the String
variable g. Then have it print this String with nothing but “small” letters.

String g = "Computer Science is for nerds";
System.out.println( g.toLowerCase( ) );

4. What will be the value of c?
String c;
String m = “The Gettysburg Address”;
c = m.substring(4);

Gettysburg Address

5. What will be the value c?
String b = “Four score and seven years ago,”;
c = b.substring(7, 12);

ore a


6. What is the value of count?
int count;
String s = “Surface tension”;
count = s.length( );

15

7. Write code that will look at the number of characters in String m = “Look here!”; and then
print
“Look here!” has 10 characters.
*Note Use the length( ) method to print the 10 ….you must also force the two quotes to print.

String theWay = "Look here!";
int rise = theWay.length( );
System.out.println(rise);



8. How would you print the following?
All “good” men should come to the aid of their country.

String d = All \“good\” men should come to the aid of their country. ";
System.out.println(d);

9. Write code that will produce the following printout using only a single println( ).
Hello
Hello again

String g = "Hello\nHello again";
System.out.println(g);

10. Write code that will produce the following printout.
A backslash looks like this \, …right?

String g = "A backslash looks like this \\, …right?";
System.out.println(g);

11. What is output by the following?
String pq = “Eddie Haskel”;
int hm = pq.length( );
String ed = pq.substring(hm - 4);
System.out.println(ed);

8

12. Which character is at the 5th index in the String “Herman Munster”?

a
Re: Learn Java Programming Here Easily by azpunpin: 8:29pm On Mar 13, 2009
THIS IS THE SOLUTION TO THE PROJECT AS I UNDERSTAND IT.

public class NameThatCelebrity
{
public static void main (String args [])
{
String s1 = "Allan Alda";
String realname = s1.substring(2, 8 );
System.out.println(realname);
String s2 = "John Wayne";
String next = s2.substring(2, 7);
System.out.println(next);
String s3 = "Gregory Peck";
String next1 = s3.substring(2, 9);
System.out.println(next1);
}
}
Re: Learn Java Programming Here Easily by azpunpin: 2:59pm On Mar 15, 2009
PLEASE, CHECK THIS CODE AND HELP ME TO FIX WHAT IS WRONG. GUYS, HELP ME OUT.!!!

It issues error @ public static void main (String args[])

public class ApplicantCollection
{
Applicant appObjects[];

public ApplicantCollection()
{
appObjects = new Applicant[3];
for(int ctr = 0;ctr != appObjects.lenght;ctr++)
{
appObjects [ctr] = new Applicant();
}

//Applicant 1 Details
appObjects[0].applicantName = "Tom";
appObjects[0].applicantAddress = "David Road";
appObjects[0].applicantPosition = "Manager";

//Applicant 2 Details
appObjects[0].applicantName = "James";
appObjects[0].applicantAddress = "David Ave Street";
appObjects[0].applicantPosition = "CEO";

//Applicant 3 Details
appObjects[0].applicantName = "Kelvin";
appObjects[0].applicantAddress = "David Road ways";
appObjects[0].applicantPosition = "Supervisor";
}
public void displayCollection()
{
for(int ctr =0;ctr != appObjects.length;ctr++)
{
appObjects[ctr].displayDetails();
}
public static void main( String args [])
{
ApplicantCollection collectionObject;
collectionObject = new ApplicantCollection();
collectionObject.displayCollection();
}
}
Re: Learn Java Programming Here Easily by javarules(m): 12:16am On Mar 16, 2009

1 public class ApplicantCollection
2 {
3 Applicant appObjects[];
4
5 public ApplicantCollection()
6 {
7 appObjects = new Applicant[3];
8 for(int ctr = 0;ctr != appObjects.lenght;ctr++)
9 {
10 appObjects [ctr] = new Applicant();
11 }
12
13 //Applicant 1 Details
14 appObjects[0].applicantName = "Tom";
15 appObjects[0].applicantAddress = "David Road";
16 appObjects[0].applicantPosition = "Manager";
17
18 //Applicant 2 Details
19 appObjects[0].applicantName = "James";
20 appObjects[0].applicantAddress = "David Ave Street";
21 appObjects[0].applicantPosition = "CEO";
22
23 //Applicant 3 Details
24 appObjects[0].applicantName = "Kelvin";
25 appObjects[0].applicantAddress = "David Road ways";
26 appObjects[0].applicantPosition = "Supervisor";
27 }
28
29 public void displayCollection()
30 {
31 for(int ctr =0;ctr != appObjects.length;ctr++)
32 {
33 appObjects[ctr].displayDetails();
34 }
35 }
36
37 public static void main( String args [])
38 {
39 ApplicantCollection collectionObject;
40 collectionObject = new ApplicantCollection();
41 collectionObject.displayCollection();
42 }
43 }


You will notice one small alteration above, your own program is missing line 35, before the public static void main(String args[]) put a }

The code still did not compile because class Applicant on line 3 can not be found.
Re: Learn Java Programming Here Easily by javarules(m): 12:18am On Mar 16, 2009
And please those of us posting code here, lets learn to put line numbers and use the "[code][/code]" tag.

Or better still post your code at www.pastebin.com, then give the url here, tx
Re: Learn Java Programming Here Easily by okunade(m): 1:19am On Mar 23, 2009
hi
i'm back
Lesson 4 coming soon
Re: Learn Java Programming Here Easily by okunade(m): 12:09pm On Apr 03, 2009
Lesson 4…, Using Numeric Variables,

The assignment operator:

The assignment operator is the standard equal sign (=) and is used to “assign” a value to
a variable.
int i = 3; // Ok,…assign the value 3 to i. Notice the direction of data flow.



3 = i; // Illegal! Data never flows this way!


double p;
double j = 47.2;
p = j; // assign the value of j to p. Both p and j are now equal to 47.2


Multiple declarations:

It is possible to declare several variables on one line:

double d, mud, puma; //the variables are only declared
double x = 31.2, m = 37.09, zu, p = 43.917; //x, m, & p declared and initialized
// zu is just declared

Fundamental arithmetic operations:

The basic arithmetic operation are +, -, * (multiplication), / (division), and % (modulus).

Modulus is the strange one. For example, System.out.println(5%3); will print 2.

This is because when 5 is divided by 3, the remainder is 2. Modulus gives the
remainder. Modulus also handles negatives. The answer to a%b always has the
same sign as a. The sign of b is ignored.

PEMDAS:

The algebra rule, PEMDAS, applies to computer computations as well. (PEMDAS stands
for the order in which numeric operations are done. P = parenthesis, E = exponents,
M = multiply, D = divide, A = add, S = subtract. Actually, M and D have equal
precedence, as do A and S. For equal precedence operation, proceed from left to right. A
mnemonic for PEMDAS is, “Please excuse my dear Aunt Sally”… See Appendix H for
the precedence of all operators.)

System.out.println(5 + 3 * 4 –7); //10
System.out.println(8 – 5*6 / 3 + (5 –6) * 3); //-5

Not the same as in Algebra !!!:
An unusual assignment….consider the following:

count = count +3; //this is illegal in algebra; however, in computer science it
//means the new count equals the old count + 3.

int count =15;
count = count + 3;



System.out.println(count); //18

Increment and Decrement:
The increment operator is ++, and it means to add one. The decrement operator is --, and
it means to subtract one:

x++; means the same as x = x +1;
x--; means the same as x = x – 1;
x++ is the same as ++x (the ++ can be on either side of x)
x-- is the same as --x (the -- can be on either side of x)

int y = 3;
y++;
System.out.println(y); //4

Compound operators:

Syntax Example Simplified meaning
a. +=
x += 3; x = x + 3;

b. -=
x -= y - 2;
x = x – (y - 2);

c. *=
z*= 46; z = z * 46;

d. /=
p/= x-z; p = p / (x-z);

e. %=
j%= 2 j = j%2;

Code Examples

int g = 409;
g += 5;
System.out.println(g); //414

double d = 20.3;
double m =10.0;
m*=d –1;
System.out.println(m); //193.0


The whole truth:
Actually, the full truth was not told above concerning x++. It does not always have the
same effect as does ++x. Likewise, x-- does not always have the same effect as does --x.

x++ increments x after it is used in the statement.
++x increments x before it is used in the statement.




Similarly,

x-- decrements x after it is used in the statement.
--x decrements x before it is used in the statement.

Code Examples

int q = 78;
int p = 2 + q++;
System.out.println(“p = ” + p + “, q = ” + q); //p = 80, q = 79

int q = 78;
int p = ++q + 2;
System.out.println(“p = ” + p + “, q = ” + q); //p = 81, q = 79

Integer division truncation:
When dividing two integers, the fractional part is truncated (thrown away) as illustrated
by the following:

int x = 5;
int y = 2;
System.out.println(x / y); //Both x and y are integers so the “real” answer of 2.5
//has the fractional part thrown away to give 2





Exercise on Lesson 4

Unless otherwise directed in the following problems, state what is printed. Some of these
problems may have incorrect syntax and in those cases you should answer that the code would
not compile.


1. int h = 103;
int p =5;
System.out.println(++h + p);
System.out.println(h);


2. Give three code examples of how to increment the integer j by 1.


3. double def;
double f = 1992.37;
def = f;
System.out.println(def);


4. Write a single line of code that will print the integer variable zulu and then decrement its
value by 1.


5. int a = 100;
int b = 200;
b/=a;
System.out.println(b + 1);


6. Write a single line of code that uses the compound operator, -=, to subtract p-30 from the
integer value v and store the result back in v.


7. Write a single line of code that does the same thing as #6 but without using - =.


8. int p = 40;
int q = 4;
System.out.println(2 + 8 * q / 2 - p);


9. int sd = 12;
int x = 4;
System.out.println( sd%(++x) );
System.out.println(x);


10. int g;
3 = g;
System.out.println(++g*79);
What is the result?


11. On a single line of code declare m, b, and f to be double and on that same line initialize
them all to be 3.14.


12. On a single line of code declare x, y, and z all to be of integer type.


13. int m = 36;
int j = 5;
m = m / j; // new m is old m divided by j
System.out.println(m);
What’s printed?


14. System.out.println(3/4 + 5*2/33 –3 +8*3);
What’s printed?


15. What is the assignment operator?


16. Write a statement that stores the remainder of dividing the variable i by j in a variable
named k.


17. int j = 2;
System.out.println(7%3 + j++ + (j – 2) );


18. Show three different ways to decrement the variable j.





Project… Cheating on Your Arithmetic Assignment

Create a new project called ArithmeticAssignment with a class called Tester that will calculate
and print the results of the following arithmetic problems:

79 + 3 * (4 + 82 –68) – 7 +19

(179 +21 +10) / 7 + 181

10389 * 56 * 11 + 2246

The printout should look like the following:

79 + 3 * (4 + 82 - 68) -7 + 19 = 145

(179 + 21 + 10) / 7 + 181 = 211

10389 * 56 * 11 + 2246 = 6401870
Re: Learn Java Programming Here Easily by okunade(m): 12:39pm On Apr 03, 2009
Lesson 5…, Mixed Data Types, Casting, and Constants

So far we have looked mostly at simple cases in which all the numbers involved in a
calculation were either all integers or all doubles. Here, we will see what happens when we
mix these types in calculations.

Java doesn’t like to lose data:
Here is an important principle to remember: Java will not normally store information in a
variable if in doing so it would lose information. Consider the following two examples:

1. An example of when we would lose information:

double d = 29.78;
int i = d; //won’t compile since i is an integer and it would have to chop-off
// the .78 and store just 29 in i….thus, it would lose information.

There is a way to make the above code work. We can force compilation and
therefore result in 29.78 being “stored” in i as follows (actually, just 29 is stored
since i can only hold integers):

int i = (int)d; //(int) “casts” d as an integer… It converts d to integer form.

2. An example of when we would not lose information:

int j = 105;
double d = j; //legal, because no information is lost in storing 105 in the
// double variable d.

The most precise:
In a math operation involving two different data types, the result is given in terms of the
more precise of those two types…as in the following example:

int i = 4;
double d = 3;
double ans = i/d; //ans will be 1.33333333333333…the result is double precision

20 + 5 * 6.0 returns a double. The 6.0 might look like an integer to us, but
because it’s written with a decimal point, it is considered to be a floating point
number…a double.

Some challenging examples:
What does 3 + 5.0/2 + 5 * 2 – 3 return? 12.5

What does 3.0 + 5/2 + 5 * 2 – 3 return? 12.0

What does (int)(3.0 + 4)/(1 + 4.0) * 2 – 3 return? -.2

Don’t be fooled:
Consider the following two examples that are very similar…but have different
answers:


double d = (double)5/4; //same as 5.0 / 4…(double) only applies to the 5
System.out.println(d); //1.25

int j = 5;
int k = 4;
double d = (double)(j / k); //(j / k) is in its own little “world” and performs
//integer division yielding 1 which is then cast as
//a double, 1.0
System.out.println(d); //1.0

Constants:
Constants follow all the rules of variables; however, once initialized, they cannot be
changed. Use the keyword final to indicate a constant. Conventionally, constant
names have all capital letters. The rules for legal constant names are the same as for
variable names. Following is an example of a constant:

final double PI = 3.14159;

The following illustrates that constants can’t be changed:

final double PI = 3.14159;
PI = 3.7789; //illegal


When in a method, constants may be initialized after they are declared.

final double PI; //legal
PI = 3.14159;

Constants can also be of type String, int and other types.

final String NAME= “Peewee Herman”;
final int LUNCH_COUNT = 122;


Project… Mixed Results
Create a new project called MixedResults with a class called Tester. Within the main method
of Tester you will eventually printout the result of the following problems. However, you
should first calculate by hand what you expect the answers to be. For example, in the
parenthesis of the first problem, you should realize that strictly integer arithmetic is taking
place that results in a value of 0 for the parenthesis.

double d1 = 37.9; //Initialize these variables at the top of your program
double d2 = 1004.128;
int i1 = 12;
int i2 = 18;

Problem 1: 57.2 * (i1 / i2) +1
Problem 2: 57.2 * ( (double)i1 / i2 ) + 1
Problem 3: 15 – i1 * ( d1 * 3) + 4
Problem 4: 15 – i1 * (int)( d1 * 3) + 4
Problem 5: 15 – i1 * ( (int)d1 * 3) + 4



Your printout should look like the following:

Problem 1: 1.0
Problem 2: 39.13333333333333
Problem 3: -1345.39999999999
Problem 4: -1337
Problem 5: -1313



Exercise on Lesson 5

Unless otherwise instructed in the following problems, state what gets printed.

1. Write code that will create a constant E that’s equal to 2.718.


2. Write the simplest type constant that sets the number of students, NUM_STUDENTS,
to 236.


3. What’s wrong, if anything, with the following code in the main method?
final double Area;
Area = 203.49;


4. int cnt = 27.2;
System.out.println(cnt);
What’s printed?


5. double d = 78.1;
int fg = (int)d;
System.out.println(fg);
What’s printed?


6. Is double f4 = 22; legal?


7. The following code stores a 20 in the variable j:
double j = 61/3;
What small change can you make to this single line of code to make it print the “real”
answer to the division?


8. System.out.println( (double)(90/9) );


9. System.out.println(4 + 6.0/4 + 5 * 3 – 3);


5-4

10. int p = 3;
double d = 10.3;
int j = (int)5.9;
System.out.println(p + p * d – 3 * j);


11. int p = 3;
double d = 10.3;
int j = (int)5.9;
System.out.println(p + p * (int)d – 3 * j);


The following code applies to 12 – 15:

int dividend = 12, divisor = 4, quotient = 0, remainder = 0;
int dividend2 = 13, divisor2 = 3, quotient2 = 0, remainder2 = 0;
quotient = dividend/divisor;
remainder = dividend % divisor;
quotient2 = dividend2 / divisor2;
remainder2 = dividend2 % divisor2;

12. System.out.println(quotient);


13. System.out.println(remainder);


14. System.out.println(quotient2);


15. System.out.println(remainder2);


16. Write a line of code in which you divide the double precision number d by an integer
variable called i. Type cast the double so that strictly integer division is done. Store
the result in j, an integer.


17. Suppose we have a line of code that says

final String M = “ugg”;

Later in the same program, would it be permissible to say the following?

M = “wow”;

Re: Learn Java Programming Here Easily by azpunpin: 11:09am On Apr 05, 2009
@Okunade.
I was waiting for you to comment on my assigment which you didn`t. Let me know if i got the assignment and the project right for the lesson 3.

I am still learning so, keep teaching.
Re: Learn Java Programming Here Easily by blacksta(m): 12:34am On Apr 11, 2009
mr okunade - i think you are going to fast you said you were going to explain the purpose of

public static void main(String[] args) why and when we need it

you have also not explained the purpose of " Constructor"


thanks
Re: Learn Java Programming Here Easily by javaprince(m): 5:24pm On Apr 12, 2009
I was get bored when I see stuffs like let me teach you Java on an online forum. Tutorials like this are always very hard to follow, contain some untested code, and dn so quick that it lacks so much details.

If you want to learn Java Programming language, just do the following simple steps.

1) Download the Java Development kit (jdk 1.6) from java.sun.com
2) Get yourself a text Editor any of TextPad, WordPad, NotePad or simple vim. textpad.com
3) Download the tutorial suite from http://java.sun.com/docs/books/tutorial/reallybigindex.html

Thats all you need. later you see that you want to get an IDE such as netbeans 6.5.1 at netbean.org or Eclipse. But i'll recommend netbeans.

Cheers.
Re: Learn Java Programming Here Easily by DEBOLABI: 7:17pm On Apr 14, 2009
i will love to know java programming, i have the prototype but i dont know programming and i;m really sad about it. please i will appreciate it if you can teache me. you can reach me on 08027656939. thanks
Re: Learn Java Programming Here Easily by blacksta(m): 10:05pm On Apr 15, 2009
DEBOLABI:

i will love to know java programming, i have the prototype but i dont know programming and i;m really sad about it. please i will appreciate it if you can teache me. you can reach me on 08027656939. thanks


Why dont you follow javaprince instructions.

(1) (2) (3)

How To Hack Bank Account In Nigeria In 30 Minutes / How Long Does It Really Take To Learn Programming( I Want To Know) / Should We Make Mobile Apps Or Web Apps For The Nigerian/African Market?

(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. 136
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.