₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,331,015 members, 8,448,247 topics. Date: Monday, 20 July 2026 at 04:38 AM

Toggle theme

Timtoday's Posts

Nairaland ForumTimtoday's ProfileTimtoday's Posts

1 2 (of 2 pages)

ProgrammingRe: Coding Challenge For Fun by timtoday: 5:42am On Oct 16, 2017
edicied:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Details {

public void countDupChars(String str){


Map<Character, Integer> map = new HashMap<Character, Integer>();

char[] chars = str.toCharArray();

for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}

Set<Character> keys = map.keySet();


for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char "+ch+" "+map.get(ch));
}
}
}

public static void main(String a[]){
Details obj = new Details();
obj.countDupChars("hello"wink;

}
}

Better than the previous java solution, but if all the character is not in the same case, your solution would fail for entry with same character but different cases I.e lower and upper cases which are supposed to count as same.
ProgrammingRe: Help With C by timtoday: 11:48am On Nov 12, 2016
What is wrong with your code:

1. Check how you are using the function
scanf(...)

remove the newline
"\n"
you are using. scanf is not the same as printf, though they might be similar.

2. Why is your average though declared to be float has integer division? Integer division doesn't given you float "results".
With integer division the "decimal" part is truncated. So you might have to cast one of the operand of the integer division as float
to get what you want. Of course, you will get your answer as "float" with decimal places and ZEROs without casting. But that would
not be the right answer.

What you don't NEED.
Throw out the header
 #include <stdlib.h>
because you don't need it here and it will make your file unnecessarily large.
Why is that? I will allow you to search that out.
My point, use only what you need.

Lastly, format your code properly using spacing. It makes your code easy to read.

Of course, I could have just corrected your code and repost. But I would not have helped you and but now that you have
to make the changes yourself. When you get it I would not have robbed you of the joy of accomplishment.

Hope this helps.
ProgrammingRe: Can An Array In Java Hold Multiple Types? by timtoday: 9:51am On Oct 06, 2016
seunthomas:
There is no point trying to reason with you.
LOL.
I don't expect you to! Please just don't call me up on ridiculous post like this!
ProgrammingRe: Can An Array In Java Hold Multiple Types? by timtoday: 9:37am On Oct 06, 2016
seunthomas:
You are actually right and wrong at the same time. In my array at compilation time, i am putting in primitive types which are all different types into the array. Simply am inserting totally different types at compilation and the compiler naturally would throw errors but because like i said in my other threads concept of "everything is an object in java" (Which was also argued at by @timtoday) the compiler actually accepts this values.

But the magic you describe at runtime, where the JVM now allocates enough memory to hold a generic object for our array.

But in plain text and for a layman, our array is taking different types but the JVM is converting it into java objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.
Good morning everyone.

@seunthomas, Please you probably should not have brought me up into this.
I saw your post, though I went through it there was no need making a comments.
Why? Because it is obvious you are living in your own reality with alot of false claims.
But since you called me up by name, I will yet help you see clearly that you don't know
what you claim you know.

Secondly, am responding because I felt you think that those who post more are likely to be
right or correct. Multiplying words on falsehood doesn't make such claims correct and the loudest person
in the room is not the usually the correct ones!

So, let me try one more time and see if you will get it.

@larisoft, mentioned to you that your code "WORKED" because
by making your objects extend one parent object,
or implement a common interface, but then, they wouldn't be 'different types' anymore.
Your response:

Then check this and explain if your assertion is true.
class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());

}

}



My Comments:
If anyone runs that code of yours but have no idea what his going on such will immediately conclude
like you, that your answer to @FincoApps was TOTALLY right when you said
They are actually not of type object rather the primitive types are upcasted to their prevalent java.lang type.
But of course, with a little pain of code, the obvious is revealed! How?

Instead of using this

System.out.println(s[0].getClass().getName());
System.out.println(s[1].getClass().getName());
System.out.println(s[2].getClass().getName());


like you did, do this


System.out.println(s[0].getClass().getSuperclass().getSuperclass().getName());
System.out.println(s[1].getClass().getSuperclass().getSuperclass().getName());
System.out.println(s[2].getClass().getSuperclass().getSuperclass().getName());


Maybe then you will get what I have explained to you in the previous post and what others are
now telling you.

They are all ACTUALLY a type of Object class. ARRAY in Java has a single TYPE!

Secondly, you also said this
There is actually a class Array which is an extension of the primitive type [] and that can be extended.
Array "class" by design take the type they "refer" and as such are instances of such type and "sir" you can't extended,
of course nothing is impossible. Maybe you want to extend Java itself! Array are not like conventional classes! Get that sir?
And I don't think you can extend class java.util.Arrays. Please NOTE the "S".

Still on this your assertion:
They are actually not of type object rather the primitive types are upcasted to their prevalent java.lang type.
ABBA Oga sir?! shocked
The primitive types, you gave in your code are NOT just UPCASTED! They were autoboxED in other words wrapped
if you will into their reprepective Wrapper Classes, which in turn are autoupcast into their MOTHER (or super if you will) class.
That is why I have use the methods getSuperclass twice to make you see it!

But if you had used instanceof like I told you in the previous post maybe you would have "healed" of this confusion!
I don't expect you to anyway!

Now to the part where you called me up!
You are actually right and wrong at the same time. In my array at compilation time, i am putting in primitive types which are all
different types into the array. Simply am inserting totally different types at compilation and the compiler naturally would throw
errors but because like i said in my other threads concept of "everything is an object in java" (Which was also argued at by @timtoday)
the compiler actually accepts this values.

But the magic you describe at runtime, where the JVM now allocates enough memory to hold a generic object for our array.

But in plain text and for a layman, our array is taking different types but the JVM is converting it into java
objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.
@larisoft was right and wrong at the same time? But you didn't tell where he was wrong and where he was right!
You are only trying to play safe!
More so, the reason why your code works was NOT because "everything is an object in java", Please stop saying
that it make you look ____. I can't say it because am 2 yrs old remember? A lang with primitive types, that needed
a wrapper class of it's type wouldn't be such where "everything is an object"!

Oh! because the compilers didn't scream out with errors makes you believe "everything is an object in java"?
If only, you have been writing Java since "Odun gbogboro" like you said, you should have know that before
Java 1.5 i.e Java5, the compiler would have handed you errors, but thanks to autoboxing and her cousins!

Moreso, in plain text, (and if you will always tell layman the absolute truth, cos they will find out with time!).
Which makes this statement
But in plain text and for a layman, our array is taking different types but the JVM is converting it into java
objects(which by the way happens to every primitive type) during runtime. So irrespective of if you do this:

int x[]=int[3];
the JVM will still convert into the associated Java type Integer which extends object.
of yours NOT TRUE!

What you called "conversion" doesn't happen EXPECT under some conditions, which is not part of your last quote.
You might have to go check that out! Am sure you will tell me you have read it all! Which is obvious you have not!

Try get the class name of "x" as you have it and check it's super class, then input primitive type int into the
array and try to dereference it like we did with the Object array previously!

Seriously, it would do you good to REALLY know what you are taking about! Or if you are Satisfied with where you are
please just stay in the thread where all you do is chest-beating and abuses. And if you must come up with some
study answering questions please just DON'T bring me in!

No, i don't hate you, in fact, you make me laugh hard with several of your "funny" assertion. But I hate to stay up hitting the same
point for several post!
ProgrammingRe: A Question On 'java: How To Program' Deitel. by timtoday: 10:37pm On Oct 05, 2016
FrankLampard:
Anytime I go to CMS I will ask for the price.
Or you might help him get that from Ojuelegba. It might be a bit costly at CSM, my opinion though based on my experience the last time I was in Lagos in August.
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday:
seunthomas:
Some people will just open their mouth to sha talk sha.

Please knowledgeable Java programmer, explain this code which is an array of objects and holds different types of object:

class Test{



public static void main(String args[]){
boolean status=true;
char c='Y';
int v=100;
Object s[]=new Object[3];
s[0]=status;
s[1]=c;
s[2]=v;

System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);

}
@lekropasky Please don't just disturb yourself over this guy. It is obvious he knows nothing!

He was finding it difficult to understand simple concepts in Java, yet he boast of writing his own JVM in 2006! Yet he didn't know that EVERY CLASS in Java EXTENDS the "class" Object EXCEPT Object itself.. Which will make ANY instance of ANY CLASS, a TYPE of "Class" Object. Which still SHOUT to the ignorance of the Methuselah programmer that an ARRAY in Java takes only a SINGLE TYPE!

Please note, I didn't say TYPES our Methuselah programmer did!

Methuselah programmer, Sir, Why change to the statement below?
which is an array of objects and holds different types of object
But you initially said those were PRIMITIVES TYPES! So now they are different types of object? So, I have educated you now a bit, but shame will not allow you to be humble. Let me still help your arrogant ignorance a bit more.

Remove the word "different" here they are all TYPE of Object,, all you need to do to know this is, just use instanceof to do a test and you will be healed totally!
Hope you know how to use that?! Let me not take the trick here is how
if (s[0] instanceof Object) System.out.println("It is an Object!"wink;


Lastly, I dare you @seunthomas To quote the very first sentence of this Oracle Java Tutorial https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and prove me wrong!

But if you will not, please don't reply me! I know you will and further show how empty you are on Java! Selah!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 1:55am On Oct 04, 2016
seunthomas:
Ok ohh. Mr only single types can be stored in a Java Array.
Ignorance is a killer.
Documentation are made for humans. Please read it. It will help you to stop making yourself a laughing stock on this thread. I pointed you to one, not to stackoverflow, but your arrogance will not allow you to go check.

Am sure you didn't know what the method getClass() returns that is why you are saying
..Class and Objects are different..
as if you are raising a revelation.

Go educate yourself properly with the tools, you claim you know. And stop showing us that you have wasted 3 yrs of your life learning nothing! Than cut and paste!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 1:36am On Oct 04, 2016
seunthomas:
Shaking my head right now.

You must be really young.

Cos you just want to win even when you are wrong.

I hope you wont go outside and do this.

By the way you are all wrong. Class and Objects are different.
You see, you really have low self-esteem. Age has got nothing to do with showing you that. Am actually 2 yrs old, here you are struggling with a 2yr old. It is not winning an argument. That is your area of specialization.

By the way you are all wrong. Class and Objects are different.
But you should have shown me HOW naw, clearly defining what both are. But as usual, you must change the point of discussion, since it is obvious you don't know what you are talking about.

For your information, an "object" "of a class" is an instance of that class. Am sure I have made mention of that again and again. Using the word "instance"
But in your mind, one who had shown you times and again that he knows what is talking about DOES NOT KNOW THE DIFFERENCE!

Obviously you live in your own reality! That is why you will claim "Everything in Java is an OBJECT!" Remember am 2 yrs Old. Please take your own advise and probably go sleep with it.
I hope you
Methuselah
wont go outside and do this.
!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 1:12am On Oct 04, 2016
seunthomas:
@timtoday I know am correct am just trying to rub it in well, so you can further show your folly.

Abi you think its beans to learn just one language for several years??
I pick up a new language in 3 days max but spent years learning Java because it was key to understanding OOP.
I have told you, leave those your self praise singing to those it will catch.. Am not in that league!
If you have to pat your ego in a public forum, you problem have a low self-esteem.

I will help one more time maybe you can see clearly and more especially for those who may want to follow your wrong side of the books, which only God knows where they are from.


class Demo {

public static void main (String[] args) {

Object b = new Boolean(false); // why is this possible
System.out.println(b);
System.out.println(b.getClass());

boolean b2 = true;
// uncomments the following lines to get the errors
//System.out.println(b2); // what type is this?
//System.out.println(b2.getClass()); // can you do this?

// what about this?
Object b3 = b2;
System.out.println(b3); // what type is this now?
System.out.println(b3.getClass()); // can you do this now?
}
}


I don't need to tell you am right. My points and posts justify itself...

Just make sure you read the link on array I linked to in one of my post and stop telling yourself you know, then it is obvious you know NOTHING!
Good Morning!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 12:50am On Oct 04, 2016
seunthomas:
JEEEZ.

I am tired of arguing with you.

Next time, try to know before arguing.
You are the one that don't know yet you are shouting everywhere! I know you will jump the gun!

I just caught you using your own code! I just don't write codes, I understand what I write unlike you, who believe everything is an object in Java!

I have demonstrated to you every time you raise a point that I know what is happening under the hood! Not just talk, talk...

Please next time. Read well before you answer questions.

Arrays in Java holds a fixed number of values of a single type. It can't be more clearer. Every other object which extends an Object is an Object! It is still a single type! Can that be so difficult? And it is not a primitive datatype. Which is one of the thing makes everything in Java NOT to be an Object. The other is it has static types. Awosh!!!!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 12:42am On Oct 04, 2016
seunthomas:
Seriously.....huh

Dude cant you just be honourable and agree you are wrong??

By the way my array is holding primitives

It is this kind of attitude that makes one rude and talk to people.

Just admit you are wrong and its gone.
I should admit am wrong? When you don't even know what you are saying? Holding primitives indeed!!!
Bro, you don't know these things! Just go and sleep!

Google can help you. Just ask is everything in Java an Object?!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday:
seunthomas:
class Test{



public static void main(String args[]){
Object s[]=new Object[3];
s[0]=true;
s[1]="Hello World";
s[2]=20;

System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);

}


}


Check that out and lets continue that argument.

Thats an array that holds different primitive types.
Like I mentioned, you might know some other language NOT Java.
Because if you know Java and how JVM works, you will not that what you have in the are not "primitive" types.
It is has been converted to the Wrapper Class Type of the primitive type I mentioned to you.

In fact, using a String should have prompted you to know that. Because String is not a PRIMITIVE TYPE, and She is immutable. At anytime you call String name = "My Name"; What you are doing REALLY is
String name = new String("My Name"wink;
.

Let me give you a taste of it using your own code do this:


public class Test{



public static void main(String args[]){
Object s[]=new Object[3];
s[0]=true;
s[1]="Hello World";
s[2]=20;

System.out.println(s[0].getClass()); //class java.lang.Boolean
System.out.println(s[1].getClass()); //class java.lang.String
System.out.println(s[2].getClass()); //class java.lang.Integer
System.out.println();

System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);


You see yourself?
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 12:32am On Oct 04, 2016
seunthomas:
And an array of objects is what exactly?? Object s[]={}

Can this hold multiple types or not?
Ahhhhaaa.. multiples of what types? Every Object defined in Java extends the "Mother" object called "Object".

Which means that every class is a sub-class of the super class Object.
And as far back as 10.19pm last night. I mentioned to you that
..Storing different sub-classes of a super-class in the ARRAY of that super-class is not the same as storing different objects of DIFFERENT types like you have in Perl, JavaScript and the likes...
So what point are you making?

In fact, in that same post of 10.19pm I said this
If you inherit from a class say Employee, with a subclass say HourlyEmployee. The best you can have stored in the ARRAY instant object of Employee is HourlyEmployee, you can't store into it a class TYPE of class say Student. And that is if you can even instantiate Employee to start with, that is if Employee is not an abstract class or an interface.

In the same vein, you can't store Strings in the ARRAY of TYPE Integers or Doubles... It doesn't work in JAVA that way, like I said in my previous post.
Of what difference is what I said and what you are bringing up now? Some 2 hrs afterwards? Common!!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 11:53pm On Oct 03, 2016
seunthomas:
Arrays can be of single type or can hold multiple types.
The high-lighted words of yours is WRONG!! And I have shown that even in my first post!

seunthomas:
Its simple, a derivative of a type is also a "type of that type", so a derivative of array is also a type of array.

If you dont believe this to be true, i can do a quick code to show you what i mean.

A derivative of array that can hold multiple objects.
Go back and read what I posted as far back as 10.19pm. You have not said anything new! Stop playing on words.


seunthomas:
I think your confusion is simple, you see array only as "[]" and not class called "Array" which is a derivative of [].
Beautiful, you mentioned that you "THINK"! Well let me help you see properly so that you will get it that you are the one confused not me!
Check here https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
it will clear up your confusion.

The documentation first sentence shows who is confused here .. "..An array is a container object that holds a fixed number of values of a single type"

Read the rest yourself... You may know some other language, but java I doubt it! Maybe not as well as you think you do.
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 11:16pm On Oct 03, 2016
seunthomas:
The whole concept of OOP is that to fundamentally grasp it you have to see everything as an object.

I took a very long time to learn this concept.

I could recommend a book for you to go and read.

OOP is a programming pattern and not necessarily a language thing.

You can actually decide to write any language in OOP and you will still conform to that language syntax and semantics.

If you need more help understanding this may you can contact me directly.


But you can read Bruce Eckel's Book "Thinking in Java" or the C++ variant "Thinking in C++"
You don't get it, do you?
I really don't care the YEARS you used in learning the concept of OO. On the ARRAY stuff in Java, bros you miss am! Go back and check what I wrote, I know the OOP is not specific to a lang., However, i have been specific in this thread since what we are taking about is Java, a lang, that implement OOP.

Need more help? From whom? If with all the experience you often claim you don't know that ARRAY in Java are of single type. I think you need to come down from your high horse of self-importance, and take a second look at yourself and everything around you. You think more highly of yourself, than you really worth!

Contact you?! So that you will tell me that "everything in Java is an Object"? Over-the-bar!

As for both Bruce Eckel's Books, I have them. In fact, I can give you the third one, which is Thinking in C++, Volume 2. I bet you don't know there are two volumes to that book!

So, thanks. No thanks. I think I have to say good-nite... I better be watching a movie than having this discussion.
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 10:47pm On Oct 03, 2016
seunthomas:
The first thing you are ever taught in any java class is "In Java everything is an object". Are you saying this assertion is wrong. This also plays out in every object oriented language.
Whoever taught that need go check his or her book again! Am sure, you read my last post and why everything in Java is not an object! Except Ruby is now the NEW Java!

Every OOP language "implements" the OO ideals to varying degrees. If the assertion is true that all OOP lang has everything as Object, then the statement will be true that "In Cpp everything is an object"!!! I laugh in Bangladeshi!
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 10:37pm On Oct 03, 2016
seunthomas:
In java everything is an object. There is the primitive type array and an object type Array built on it. So maybe you should do some more research.
I don't need to do any research, I gave you examples of what am saying. Maybe you need to do more research as everything in Java is NOT an OBJECT. Java is not Ruby! Please don't get it confused.
Every primitive type has what is called a Wrapper Class Type which is an Object. Though these can take the primitive datatype but it does mean that the primitive datatype in Java is an Object.

More so, there is a class called Arrays, from java.util.Arrays. Used to make working with arrays of different datatypes a bit easier. That also doesn't make an ARRAY of a TYPE a datatype in itself. So what other research, do you think you will like to recommend?

In Java, an ARRAY of a type holds a single type! Plain and simple! Please don't complicate things.
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 10:19pm On Oct 03, 2016
seunthomas:
The beauty of inheritance in object oriented programming is that you can actually create new types from one type.

Therefore you can have a class inheriting from type Array and it will able to store different objects.
Am sure you know that array is not a TYPE in itself.
If you inherit from a class say Employee, with a subclass say HourlyEmployee. The best you can have stored in the ARRAY instant object of Employee is HourlyEmployee, you can't store into it a class TYPE of class say Student. And that is if you can even instantiate Employee to start with, that is if Employee is not an abstract class or an interface.

In the same vein, you can't store Strings in the ARRAY of TYPE Integers or Doubles... It doesn't work in JAVA that way, like I said in my previous post.

Storing different sub-classes of a super-class in the ARRAY of that super-class is not the same as storing different objects of DIFFERENT types like you have in Perl, JavaScript and the likes. Except you would like to show that all datatypes are the same as regards "vanilla" ARRAYs.

One of the reasons for other "containers" like we have it C lang., and her cousins like Cpp, Java and the likes.
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 10:02pm On Oct 03, 2016
silento:
I don't know for java but in c language

enum is defined constant name for integer value for easy remembrance for the programmer

array is a collection of data or value address

in a real word

we know that we have 12 month in a year and enum of months can go like this

enum month{jan,Feb,...}

and
an array may contain how many days are in each month
ex.
days[12]={30,31,34,....}

to access how many days that is in February you can just like

days[Feb]

instead of remembering that February is the second month of the year


so with this example

enumeration type is a defined name for an integer value for easy remembrance
You are right.
The beauty of all these is that Java took C lang., to another level by even making it alot easier to use enum.

Compare this:

Code in C lang.


#include <stdio.h>

#define MONTHS 12

typedef enum Month {
JAN = 1, FEB, MAR
} Month;

int main(void) {

int Days[MONTHS] = {31, 28, 31,};
Month month = FEB;

printf("%-5d %3d\n", month, Days[month-1]);

return 0;
}


Note that it is the programmers responsibility, to take care of the "one-off" since, array and enum in C, will start from 0. Same in Java, but it is all take care off.

Code in JAVA

class MonthDemo {
public static void main (String[] args) {
Month month = Month.FEB;
System.out.print(month.prints());
}
}

enum Month {
JAN(31),
FEB(28),
MAR(31);

private int days;

Month(int days) {
this.days = days;
}

String prints() {
return String.format("%-5s %3d\n", this, this.days);
}
};
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 8:43pm On Oct 03, 2016
elfico:
Thanks. I actually posted as per general rule. I am not conversant with java. In javascript, array holds multiple types. In C#, arraylist also holds multiple types, but array must specify the type they hold and I believe this holds true especially for strongly-typed languages.
In Java, the likes of ArrayList<type>() also holds multiple types unlike the "vanilla" or plain array, which of course is the first data structure programmers are introduced to in so many programming languages.
ProgrammingRe: What The Different Between ENUM And ARRAY In Java by timtoday: 8:39pm On Oct 03, 2016
seunthomas:
@elfico is actually right on enum but i want to slightly differ on arrays. Arrays may not necesarily always store the same type. There are specialized arrays that can hold any value but they still follow basic rules guiding array datatype.
I disagree in this sense ...
A "vanilla" array will store the same type in JAVA and a few of her cousins like C and C++.

E.g

final int SIZE = 10;
int[] numbers = new int[SIZE]; // you can't include Strings


Even if you write a class and make an array of it. Though the object of that class consist of various datatypes, you will still only be able to store the "type" of that class in it's array, even if you make a subclass of it.

E.g


class PersonDemo {
public static void main(String[] args) {
Person[] human = new Person[3];

human[0] = new Person("Name", 12);
human[1] = new Person("My Name", 15);

human[2] = new Person(false, null); // you can't do this
}
}

class Person {
private String name;
private int age;

Person(String name, int age) {
this.name = name;
this.age = age;
}
// set and do other stuff...
String getName() {
return this.name;
}
};

It is quite different to what one can do in languages like Perl, Python and some others.
Just saying...
ProgrammingRe: Lets Learn C by timtoday: 12:19am On Aug 18, 2016
stack1:
might not post for a few day's, almost out if data here, but by or b4 weekend would be back
Hello,

Take your time..

On the upgraded program I think it works better now..

And like I mentioned previously, one can also do this:


#include <stdio.h>

int main(void) {

int ch, temp;

while ((ch = getchar()) != EOF) {

// get the beginning of the comments
if (ch == '/') {
temp = ch; // temp variable
ch = getchar();

// for multiple line
if (ch == '*') {
ch = getchar();
while(ch != '*')
ch = getchar();

ch = getchar();
while (ch != '/')
ch = getchar();
}

// for a single line of comment C++ style
else if (ch == '/') {
ch = getchar();
while(ch != '\n')
ch = getchar();
putchar('\n');
}
else {
putchar(temp);
putchar(ch);
}
}
else {
putchar(ch);
}
}

return 0;
}


The above should also work as intended.. So, enjoy yourself... later bro...
ProgrammingRe: Lets Learn C by timtoday: 8:52am On Aug 17, 2016
stack1:
ok .. so waiting for test's and confirmation from anyone on the modified program before i proceed with the explanation
Stack1,
Well done. I see your comments. Like I said we would be using all manner of "Brute force" solutions handpicking char, instead of token solution. We could even give a solution without using either bool datatype or an array like you are using.

But it will be many "key-stoke" solution.

More over, I saw that you have "stylishly" updated the comments having /**** UNIX/LINUX ***/ grin But I have not tested the modified code to see if it works...

Nice work bro, I will check back later... got to sleep!
ProgrammingRe: Lets Learn C by timtoday: 2:12am On Aug 17, 2016
stack1:
Hi guyz, been a while. Sorry for the long break but i've been so busy with stuff.. Initially i thought about going into Formatted input and Output tonight, however we have covered quite some basics, so i felt why not throw in a relatively ambitious example and lets tackle it, before going into more advanced stuff

Languages belonging to the C family like C (itself), C++, Java, Go, JavaScript e.t.c, have certain similar features, like they all use both
single line comments

// coment goes here ..this are also known as C++ style comments


and multi-line comments


/*
* Lotta comments here. blah blah
*/


Generally comments are meant for human readers to better understand your code or to provide a means to generate documentation using various tools, so the compiler/interpreter generally ignores and discarded all comments..
The code below is an attempt at writing a Program that would remove all Single and multi-line comments from a program source code, so try it by feeding it various commented programs (not just C programs, also Java e.t.c) that you can find anywhere and lets see if it works for you..
Also try going through the code to see if you can get a hang of it, I'll be back in a few hours to go through it thoroughly.

Also when testing the program after posting your code press Ctrl+Z to send the End-Of-File character as the program would keep reading input till it reaches EOF, then it processes the input and spits back your code with all comments removed..




/*
Program to remove comments from a C Program.
removes both C and C++ style comments
*/

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>


static unsigned char input_buffer[200000];

int main(void) {


int c,d,
in_char_count = 0;

bool m_cmt = false; //multi-line comment detected, initially false
bool s_cmt = false; //single-line comment detected, initially false



printf(" Enter Source Code Below \n\n" );


while((c = getchar()) != EOF ) {


if(m_cmt) {

c = getchar();
d = getchar();

while(c != '*' && d != '/') {
c = d;
d = getchar();
}

m_cmt = false;

c = getchar();
c = getchar();
}

if(s_cmt) {
c = getchar();
while(c != '\n') c = getchar();

s_cmt = false;

}

if(c == '/' ) {

d = getchar();

if(d == '*') m_cmt = true;

else if(d=='/') s_cmt = true;

else {
input_buffer[in_char_count++] =c;
input_buffer[in_char_count++] = d;

continue;
}
}

if(m_cmt || s_cmt) continue;
else
input_buffer[in_char_count++] = c;

}




system("clear" ); /* CLEAR SCREEN ON UNIX/LINUX */
system("cls" ); /* CLEAR SCREEN ON DOS (..ooh windows i mean ) */



printf("\n\n\nProcessed source code...\n\n" );

input_buffer[in_char_count] = '\0';

printf("%s", input_buffer);

getchar();

return EXIT_SUCCESS;


}



In this example we included a new library header file you may have not seen before, stdbool.h , this header file simply introduces the Boolean types into our program, The Boolean type is a type that can hold two values either true or false, Boolean's can be used to represent state, like an on [/b]or [b]off [/b]state, or 0 and 1, Yes and No, in the example we simply use it to track when we are in single and multi-line comments and when we are out of them... so please run the example and lets discuss it. The boolean type wasn't originally part of C, and was introduced in the C99 version of the language
Hi sir,

I came to read gossips on NL and it happened that I came across your post again... So I said let me learn small. Below is my finding after testing the program you gave.

If you test the program above with itself, ALL of the comments will be removed EXPECT this line
system("clear" ); INUX */
So, that means a RED flag was raised and the program it is not working as we wanted.

So, just as you asked I pulled in a lot of comments to see how the program will do. Like this below


/*
Program to remove comments from a C Program.
removes both C and C++ style comments
*/

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>


/****************************************************************
***** THE PROGRAME SHOULD REMOVE COMMENTS ***********************
*****************************************************************/


static unsigned char input_buffer[200000];

int main(void) {


int c,d,
in_char_count = 0;

bool m_cmt = false; //multi-line comment detected, initially false
bool s_cmt = false; //single-line comment detected, initially false



printf(" Enter Source Code Below \n\n" );


while((c = getchar()) != EOF ) {

/*
*******************************************************************
***************** We try to handle multiple lines here ************
*******************************************************************
*/



if(m_cmt) {

c = getchar();
d = getchar();

while(c != '*' && d != '/') {
c = d;
d = getchar();
}

m_cmt = false;

c = getchar();
c = getchar();
}

if(s_cmt) {
c = getchar();
while(c != '\n') c = getchar();

s_cmt = false;

}

if(c == '/' ) {

d = getchar();

if(d == '*') m_cmt = true;

else if(d=='/') s_cmt = true;

else {
input_buffer[in_char_count++] =c;
input_buffer[in_char_count++] = d;

continue;
}
}

if(m_cmt || s_cmt) continue;
else
input_buffer[in_char_count++] = c;

}




system("clear" ); /* CLEAR SCREEN ON UNIX/LINUX */
system("cls" ); /* CLEAR SCREEN ON DOS (..ooh windows i mean ) */



printf("\n\n\nProcessed source code...\n\n" );

input_buffer[in_char_count] = '\0';

printf("%s", input_buffer);

getchar();

return EXIT_SUCCESS;


}


Then the program tested again, and it failed badly!

So, I adjusted it a bit. Since the program is failing on removing multiple comments, therefore I focused on that.

Below is the rewrite if you will:



/*
Program to remove comments from a C Program.
removes both C and C++ style comments
*/

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

/****************************************************************
***** THE PROGRAME SHOULD REMOVE COMMENTS ***********************
*****************************************************************/

static unsigned char input_buffer[200000];

int main(void) {


/*****comments****/
int c,d,
in_char_count = 0;

bool m_cmt = false; //multi-line comment detected, initially false
bool s_cmt = false; //single-line comment detected, initially false



printf(" Enter Source Code Below \n\n" );


while((c = getchar()) != EOF ) {

/*
*******************************************************************
***************** We try to handle multiple lines here ************
*******************************************************************
*/
if(m_cmt) {

c = getchar();

while(c != '/') {
c = getchar();
}

c = getchar();
if ( c != '\n') continue;
m_cmt = false;
}

if(s_cmt) {
c = getchar();
while(c != '\n') c = getchar();

s_cmt = false;

}

if(c == '/' ) {

d = getchar();

if(d == '*') m_cmt = true;

else if(d=='/') s_cmt = true;

else {
input_buffer[in_char_count++] =c;
input_buffer[in_char_count++] = d;

continue;
}
}

if(m_cmt || s_cmt) continue;
else
input_buffer[in_char_count++] = c;

}

system("clear" ); /* CLEAR SCREEN ON UNIX/LINUX */
system("cls" ); /* CLEAR SCREEN ON DOS (..ooh windows i mean ) */


printf("\n\n\nProcessed source code...\n\n" );

input_buffer[in_char_count] = '\0';

printf("%s", input_buffer);

getchar();

return EXIT_SUCCESS;


}



Then tested the program against itself and I had what we wanted!
I would rather have preferred reading a whole line at a time then parse it, than hand-picking char like we are doing...

I understand that since you have not explain File I/O in C, that maybe difficult for the readers to use. since we must be tested on what we have discussed do far. That is good. But I believe parsing is a lot easier using chunks instead.

Nice one bros.... Keep Up the great work...
ProgrammingRe: Lets Learn C by timtoday: 6:23am On Aug 09, 2016
stack1:
As an example if you where writing a program calculating radius instead of creating an integer variable for PI like int PI = 3.142
its more appropriate to do
#define PI = 3.142...,
so anywhere you use PI, the value 3.142 would be substituted
While the issues of ASCII value and others in my previous post still apply to this. I will like to draw your attention to this
#define PI = 3.142....
Contrary to this statement
so anywhere you use PI, the value 3.142 would be substituted
.

No your program will NOT compile! WHY? an expression is needed before the token "=". In fact, the substitute value of PI is seen from the space that follows afterwards...

Probably is a typo! smiley You are doing great boss...
ProgrammingRe: Lets Learn C by timtoday: 6:06am On Aug 09, 2016
stack1:
This next program remove's extra spaces between text, so to try it enter any amount of text with lot's of space between the text's and the program would detect and truncate such space(s) to just one space



int main(void)
{
int c,last_char_is_blank;

last_char_is_blank = 0;

while((c=getchar())!=EOF)
{

if(c==32) //a space was entered (ASCII) could have as well used c=' '
{
if(last_char_is_blank == 0) //flag hasn't been set, so the previous character wasn't a space
{

putchar(c);
last_char_is_blank=1; //set last char was a space flag
}
}
else
{
putchar(c);

last_char_is_blank=0; //regular char input, reset this
}
}

getchar();
return 0;
}



Now lets go through it.
Note that we start by declaring two integer variables in the main function c and last_char_is_blank,
we would use the last_char_is_blank variable, as a control variable to detect multiple spaces,

We read input characters into the variable c in the while loop

Next we have the if statement if(c==32) The number 32 is the ASCII value for SPACE and we could have equally done if(c== ' ') [\b], and achieved the same result, if the character entered is a space, the if block is executed this shows that you can use any ASCII decimal value instead of an Actual character.

The next line [b] if(last_char_is_blank == 0)
checks to see if the control variable has not been set the variable last_char_is_blank is initially assigned a value of zero so this test would pass and the following statements

  putchar(c);
last_char_is_blank=1; //set last char was a space flag


would be executed, the first statement outputs the space character ( remember we're still inside the if statement that detected a space)
then the next statement sets our control variable last_char_is_blank to one

On the next run of the loop if the imputed character is again a space, the if statement detects it, and so when the check if(last_char_is_blank == 0) is executed, the test fails since last_char_is_blank would now set to 1 (provided the previous character was a space)

If the imputed character is however not a SPACE character, the first if statement fails and the else block is executed, so the non-space character gets printed. and the last_char_is_blank variable is reset to ZERO

This way multiple spaces within the text is prevented. Enter the code and run-it to fully understand
Hi Stack,
Let me first say nice work. It is not easy to put up tutorials and as such in C programming language. The language of the gods! Lol.. I know plenty talk go start from here now!

All the same, there are one or two things in my opinion that i felt is NOT sitting well as regard some of the examples you posted. At least, from this one and those that followed. Of course, there are "some-what" in the previous ones like using a char datatype to get the return value of "macro" getchar which obviously returns an integer. Of course it works because intrinsically char are "integer". And you indeed use a correct return afterwards.

That been said, checking your example above, it will only take care of "spaces" whose ASCII value is 32. "Space-like" tab and others will NOT be taken care of.
Secondly, using a "raw" value i.e the ASCII value will defeat clarity of codes. Yes, you commented the line, but plain integer 32 could also be used for other things IF the code is extended or for other reasons. Somehow or somewhere portability will be defeated.

More so, why must I cram or have to lookup ASCII values to be able to write codes? It will further drive potential C programmer's away.

Lastly, there are other difficult concepts in C already why add this to it. It is like trying to know the IP address of google instead of just using www.google.com.

What do I think?
Since, C, already gave us a header file
<ctype.h>
Let us use that to our advantage and write a more portable codes, instead of "hand-picking" values.

Using such header, allows us to test more conveniently the value we are getting from standard input or file, however the case maybe.
Below is rewrite of some sort...



#include <stdio.h>
#include <ctype.h> // isspace(int)

#define putchar(c) \
if ((c) == '\b' || (c) == '\t' || (c) == ' ' || (c) == '\v')\
putchar(' ')


int main(void) {

int c;

// we could have use bool value instead
// from the header stdbool.h
// instead of integer
int is_blanck = 0;

while((c = getchar()) != EOF) {
if (isspace(c)) {
++is_blanck;
if (is_blanck == 1)
putchar(c); /// self defined macro above
} else {
is_blanck = 0;
// note the bracket around the macro
(putchar)(c); // macro from stdio.h
}
}

return 0;
}



Really, there is (might be) NO *need* for the self defined macro included but to show that allowing the language to do it work. It a lot painless some of the time.
Just a note.
ProgrammingRe: What's Your Favourite Text Editor? by timtoday: 7:56pm On Jul 06, 2016
vim
Christianity EtcRe: What If The Early Church Had Prayed That Saul Should Die? by timtoday: 12:26am On Mar 28, 2016
nnamdiosu:
OK sir. but I have one question for u. wen we say my phone has died or my phone battery has died, does it mean that the phone died? can phone battery die? DOESNT IT MEAN THAT MY PHONE HAS SHUT DOWN OR STOPPED WORKING?
now let me.tell u one secret. prayer is simply communication between man and his God IN A PERSONAL WAY. PERSONALLY THERE ARE SOME WORDS THAT I SAY IN PRAYER TO GOD. ONLY ME AND GOD KNOWS WHAT IT MEANS. SOMEONE ELSE DOESNT KNOW. BUT GOD ANSWERS THOSE PRAYERS. let's leave it like that. God bless u sir
Sorry to say, your illustration is faulty. Why? If your phone battery is dead or your phone is dead means it has stopped to work, then you transpose that to mean that you ask an "evil spirit(s)" as the case may be, to die means to stop it activities in your life. What you are saying is that a child of God could have a demon in his/her lives, but the spirit will have no power to perform their enterprises, just like dead battery. Because, though your battery may be dead but still remain in it place until recharged again.
Mean while, what the scripture required us to do is to CAST out devils! Not ask that they lose their powers, while we endure their presence. We are to tread on serpents and scorpions, and over all the power of the enemy.

Secondly, why the battery illustration is not accurate, can the devil lose his power like a battery will lose it's charge due to usage? if so, where does he(they) refuel or recharge? Go and check in scriptures, the person through whom they operate might cease to do what they are capable of after the spirit is cast out, but the spirit itself doesn't. That is one of the reason, I know that all of the fall down and die prayers are aimed at people and not spirits, because spirits don't die to start it. Example, a so called Pastor recently prayed that El-rufai will fall down and die if he don't reverse a policy, am sure he is also talking to a spirit called El-rufai shea? He even boasted that he prophesied that Audu of Kogi State will die, and so it happened. What a pity!
Please go and read your bible, the judgement of those outside the church is not in our hands. We are sons of God NOT God!

Lastly, if you are a child of God and you still have a compartment for the devil to ocuppy, like a phone will for a battery, then it is your fault! Why? scriptures says "...Neither give place to the devil..." -- Eph 4:27. Read that passage in context.

A pastor said long ago and I agree with him, that one of the problem of the church today is biblical illiteracy. For example, who is the fire met for? The Holy "Ghost" fire is for what? To burn the devil? If so, why didn't Jesus or his disciples use it? Is Jesus not the one to baptized US with the holy spirit and fire?
Simple cast the devil out! And stop devil worship by giving him all the attention. God is no equal with Satan.
ProgrammingRe: Is It Advisable To Start Learning Programing With C++ by timtoday: 9:02am On Mar 27, 2016
Johniyk2:
Take it from me mr man, programming os a step by step learning process, for web designing start with html while for development start from javascript before others. Thanks
That programming is step by step doesn't state the language people start with. You can still learn step by step using any programming language of your choice. The only difference is the learning curves.
Everybody doesn't have to start with HTML, especially when one is not majoring in web designs. In fact, starting with those languages that people "believed" to be difficult even gives a better perspective for others "supposedly" easy ones. Which for me is the way to go. Often times the difficulties is in the saying of many.

1 2 (of 2 pages)