Jump to content


- -

* * * * * 1 votes

Offering Java Programming Help


12 replies to this topic

#1 sayedamir2000

sayedamir2000

    Member

  • Advanced Members
  • Pip
  • 137 posts
  • Religion:Islam

Posted 06 May 2013 - 02:29 PM

(bismillah)
(salam) brothers and sisters

I want also to help our community like others and give back something with my skills and shiachat have really helped me out. So I decided to offer my Java skills to help you. You can ask here in this topic and I will answer you. Reason why it would be preferred to write here is that so others can benefit from that knowledge. I'm planning in future to open own Java teaching blog and put some video tutorial and text tutorials. If you need help with some tasks I'm ready to help but nowadays I'm little bit busy so that I can't be 24h ready for everything :), so be patient and send me PM if you want to have Skype conversation :)

Enshallah I'm able to help as many members :D

By the way this doesn't mean that I will do your tasks, you do them on yourself otherwise you won't learn :) but I might be able to guide you with it :)

If you have any project based on Java technologies to be done, just send me PM and let's discuss.

Edited by sayedamir2000, 06 May 2013 - 02:39 PM.


#2 PurifiedTruth

PurifiedTruth

    خادم المهدي (Servant of our awaited Imam(AS))

  • Advanced Members
  • PipPip
  • 307 posts
  • Location:Canada
  • Religion:Quran and Ahlulbayt
  • Interests:Quran, Tafsir, Hadith, Theology, History, Astronomy , Anthropology, Engineering

Posted 06 May 2013 - 03:17 PM

(bismillah)
(salam)

Excellent thread! Jazakallah for making this brother and putting your time to spread your knowledge. It's just what I need at this time since I'm currently taking an online Java course that I need for my undergraduate major.  The course is Data Structures and Algorithms in Java coding and I'm using Netbeans as an IDE

I'll start of with my first question that I need help with.

In the first unit , I learned about the definition of LinearSearch. Here is an example on it given from the course notes that I'm trying to run but for some reason it's not working. Here is the code.

Firstly, we have an array called fruit .


String[] fruit = {"apple" , "grape" , "orange", "plum"}

int linear_search_on_fruit(){
  
for (int i = 0 ; i <fruit.length; ++i) {
if (fruit.equals("lemon")){
  return i ;
}
}
return -1 ;  // -1 means that lemon was not found


Every time I try to run it on Netbeans it gives me an error message and for some reason does not identify the first method (int linear_search_on_fruit)

Any idea why? your input will help a lot.

(wasalam)

Edited by PurifiedTruth, 06 May 2013 - 03:22 PM.

Posted Image


#3 repenter

repenter

    Born With A Vengeance

  • Mods
  • 3,114 posts
  • Location:Beyn-ul-Harameyn

Posted 06 May 2013 - 04:32 PM

View PostPurifiedTruth, on 06 May 2013 - 03:17 PM, said:

(bismillah)
(salam)

Excellent thread! Jazakallah for making this brother and putting your time to spread your knowledge. It's just what I need at this time since I'm currently taking an online Java course that I need for my undergraduate major.  The course is Data Structures and Algorithms in Java coding and I'm using Netbeans as an IDE

I'll start of with my first question that I need help with.

In the first unit , I learned about the definition of LinearSearch. Here is an example on it given from the course notes that I'm trying to run but for some reason it's not working. Here is the code.

Firstly, we have an array called fruit .


String[] fruit = {"apple" , "grape" , "orange", "plum"}

int linear_search_on_fruit(){
  
for (int i = 0 ; i <fruit.length; ++i) {
if (fruit.equals("lemon")){
  return i ;
}
}
return -1 ;  // -1 means that lemon was not found


Every time I try to run it on Netbeans it gives me an error message and for some reason does not identify the first method (int linear_search_on_fruit)

Any idea why? your input will help a lot.

(wasalam)

Your code isn't working because you are comparing the array fruit to the string. You have to compare the content of the array at "i" to the string like this:


public int linear_search_on_fruit()
{

for (int i = 0 ; i <fruit.length; ++i)
{
if (fruit[i].equals("lemon"))
{
return i;
}
}
return -1;  // -1 means that lemon was not found
}

You also have some weird way of writing your code. Normally you write i++ not ++i. Unless you have a specific reason you do this?

Edited by repenter, 06 May 2013 - 04:34 PM.


#4 PurifiedTruth

PurifiedTruth

    خادم المهدي (Servant of our awaited Imam(AS))

  • Advanced Members
  • PipPip
  • 307 posts
  • Location:Canada
  • Religion:Quran and Ahlulbayt
  • Interests:Quran, Tafsir, Hadith, Theology, History, Astronomy , Anthropology, Engineering

Posted 06 May 2013 - 09:29 PM

View Postrepenter, on 06 May 2013 - 04:32 PM, said:

Your code isn't working because you are comparing the array fruit to the string. You have to compare the content of the array at "i" to the string like this:


public int linear_search_on_fruit()
{

for (int i = 0 ; i <fruit.length; ++i)
{
if (fruit[i].equals("lemon"))
{
return i;
}
}
return -1;  // -1 means that lemon was not found
}

You also have some weird way of writing your code. Normally you write i++ not ++i. Unless you have a specific reason you do this?


Jazakllah bro. It partially works now, and the method is identified that you given is identified . However I'm still getting an error as it keeps telling me to define the main method as public static void main{String[] args}.

Every time i do that it gives me an error on the public int linear_search_fruit() method and keeps telling me that it cannot return i. Any ideas? It's from my online notes but they are really lame and most students from this course are complaining on the course discussion forums lol.

Posted Image

Posted Image


#5 sayedamir2000

sayedamir2000

    Member

  • Advanced Members
  • Pip
  • 137 posts
  • Religion:Islam

Posted 07 May 2013 - 01:57 AM

(bismillah)
(salam)

Luckily I use same IDE. I found your error and solution for it. Reason why your code is not working is because in front of your int linear_search_on_fruit(){ } you are lack of declaration of its modifier. There are three types of modifiers public, private and protected. In your case you probably want to add public modifier in order to get your code working. So this would be your code:

String[] fruit = {"apple", "grape", "orange", "plum"};

public int linear_search_on_fruit(){
for (int i = 0; i < fruit.length; i++) {
if (fruit[i]equals("lemon")) {
return i;
}
}
return -1;
}

Now in this case your code will always return -1 because just as brother repenter points out that you can't compare array values to string. Its like comparing for example number 1-10 all together to a specific number and thus its impossible to return correct value. Instead you need to compare elements and that's why you need to [i] in the end of fruit array in if loop, so it looks like fruit[i]. In this case it means that once the for loop start it will go each element of fruit and always compare it to String value you want to be compared and in this case its "lemon". :)

Now about your error that you are showing in your picture. There's one specific and easy solution for it. What you can do is this:

1. In your linear_search package create a class and you can name it whatever you want. To ease our stuff I will name this class as Main, with capitalized letter of M.
2. Add to that class main method like this public static void main(String[] args){ }
3. Create another class in SAME package and name it whatever you wish. I will name it LinearSearchOnFruit.
4. Paste your whole code in LinearSearchOnFruit class.
5. Declare new class variable in your main method like this:

public static void main(String[] args){
LinearSearchOnFruit linearSearch = new LinearSearchOnFruit();
}

6. Now Netbeans ask you to import LinearSearchOnFruit class, so you need to import it. If you click the ligh bulb in left side of your line you will be get options, just click import....[class].
7. Now you need to use that specific method in your LinearSearchOnFruit by calling it, simply like this:

public static void main(String[] args){
LinearSearchOnFruit linearSearch = new LinearSearchOnFruit();
linearSearch.linear_search_on_fruit();


}

Now it should work. That is easy and clean way.

View Postrepenter, on 06 May 2013 - 04:32 PM, said:

You also have some weird way of writing your code. Normally you write i++ not ++i. Unless you have a specific reason you do this?

Brother you can do also like that but I think in this case its wrong because brother PutriefiedTruth want probably to loop through the String by going each element on right order. But just to notify that you can do ++i, in this case it will first make addition to i.

Edited by sayedamir2000, 07 May 2013 - 01:59 AM.


#6 PurifiedTruth

PurifiedTruth

    خادم المهدي (Servant of our awaited Imam(AS))

  • Advanced Members
  • PipPip
  • 307 posts
  • Location:Canada
  • Religion:Quran and Ahlulbayt
  • Interests:Quran, Tafsir, Hadith, Theology, History, Astronomy , Anthropology, Engineering

Posted 07 May 2013 - 04:16 AM

View Postsayedamir2000, on 07 May 2013 - 01:57 AM, said:

(bismillah)
(salam)

Now about your error that you are showing in your picture. There's one specific and easy solution for it. What you can do is this:

1. In your linear_search package create a class and you can name it whatever you want. To ease our stuff I will name this class as Main, with capitalized letter of M.
2. Add to that class main method like this public static void main(String[] args){ }
3. Create another class in SAME package and name it whatever you wish. I will name it LinearSearchOnFruit.
4. Paste your whole code in LinearSearchOnFruit class.
5. Declare new class variable in your main method like this:

public static void main(String[] args){
LinearSearchOnFruit linearSearch = new LinearSearchOnFruit();
}




public static void main(String[] args){
LinearSearchOnFruit linearSearch = new LinearSearchOnFruit();
linearSearch.linear_search_on_fruit();


}

Now it should work. That is easy and clean way.


(wasalam)

Jazakallah. Thanks for your input. I read over your tips and managed to get things all connected together and I managed to import the classes as shown at the bottom of the code. However, I'm still getting an error. I think it has to do with the main method. I understand everything being done after the linear_search_on_fruit(){ } method.

Posted Image


Quote

6. Now Netbeans ask you to import LinearSearchOnFruit class, so you need to import it. If you click the ligh bulb in left side of your line you will be get options, just click import....[class].
7. Now you need to use that specific method in your LinearSearchOnFruit by calling it, simply like this:

For that, do we choose "create class "LinearSearchOnFruit" in package" or "create class "LinearSearchOnFruit" in Test"? Because for the code in the picture above, I chose LinearSearchOnFruit" in package and it opened a new class on the right side of Test.java as you can see in the picture.

Your input is appreciated.

fi mana illah

Posted Image


#7 sayedamir2000

sayedamir2000

    Member

  • Advanced Members
  • Pip
  • 137 posts
  • Religion:Islam

Posted 07 May 2013 - 04:53 AM

View PostPurifiedTruth, on 07 May 2013 - 04:16 AM, said:

(wasalam)

Jazakallah. Thanks for your input. I read over your tips and managed to get things all connected together and I managed to import the classes as shown at the bottom of the code. However, I'm still getting an error. I think it has to do with the main method. I understand everything being done after the linear_search_on_fruit(){ } method.

Posted Image




For that, do we choose "create class "LinearSearchOnFruit" in package" or "create class "LinearSearchOnFruit" in Test"? Because for the code in the picture above, I chose LinearSearchOnFruit" in package and it opened a new class on the right side of Test.java as you can see in the picture.

Your input is appreciated.

fi mana illah

Now I saw two things you can do to fix this problem.

1. You have to name your package. It can be anything but don't leave it as <default package>. In Netbeans there's a cool shortcut to rename most of files and packages :). Just select the <default package> and hit Ctrl+R. Refactor window will popup. Just add new name to your package and hit "Refactor". You can name it as anything you want. In my case I will name it as Other. Brother I made small mistake before. I'm sorry about it. Now the cool thing is that if your files are in same package which is in this case as I see your files pictures you don't need to import anything, but make sure that in your Test class above it there's following piece of code package (Package Name here);
In our case it will be package Others;

2. Don't add public int linear_search_on_fruit(){ } method to your Test class. That same method is already or at least should be in LinearSearchOnfruit class. What we are doing in Test class is just creating and calling that LinearSearchOnfruit class and using its method.

So your code should be like this now:

Test class:

package Others;

public class Test {
public static void main(String[] args) {
LinearSearchOnfruit linearSearch = new LinearSearchOnfruit();
linearSearch.linear_search_on_fruit();
}
  
}

LinearSearchOnfruit class:

package Others;
public class TestingClass {
String[] fruit = {"apple", "grape", "orange"};
public int linear_search_on_fruit() {
for (int i = 0; i < fruit.length; ++i) {
if (fruit[i].equals("lemon")) {
return i;
}
}
return -1;
}
}

Hope this helps :). It will work at least :)

Edited by sayedamir2000, 07 May 2013 - 04:55 AM.


#8 PurifiedTruth

PurifiedTruth

    خادم المهدي (Servant of our awaited Imam(AS))

  • Advanced Members
  • PipPip
  • 307 posts
  • Location:Canada
  • Religion:Quran and Ahlulbayt
  • Interests:Quran, Tafsir, Hadith, Theology, History, Astronomy , Anthropology, Engineering

Posted 08 May 2013 - 03:13 AM

View Postsayedamir2000, on 07 May 2013 - 04:53 AM, said:

Now I saw two things you can do to fix this problem.

1. You have to name your package. It can be anything but don't leave it as <default package>. In Netbeans there's a cool shortcut to rename most of files and packages :). Just select the <default package> and hit Ctrl+R. Refactor window will popup. Just add new name to your package and hit "Refactor". You can name it as anything you want. In my case I will name it as Other. Brother I made small mistake before. I'm sorry about it. Now the cool thing is that if your files are in same package which is in this case as I see your files pictures you don't need to import anything, but make sure that in your Test class above it there's following piece of code package (Package Name here);
In our case it will be package Others;

2. Don't add public int linear_search_on_fruit(){ } method to your Test class. That same method is already or at least should be in LinearSearchOnfruit class. What we are doing in Test class is just creating and calling that LinearSearchOnfruit class and using its method.

So your code should be like this now:

Test class:

package Others;

public class Test {
public static void main(String[] args) {
LinearSearchOnfruit linearSearch = new LinearSearchOnfruit();
linearSearch.linear_search_on_fruit();
}
  
}

LinearSearchOnfruit class:

package Others;
public class TestingClass {
String[] fruit = {"apple", "grape", "orange"};
public int linear_search_on_fruit() {
for (int i = 0; i < fruit.length; ++i) {
if (fruit[i].equals("lemon")) {
return i;
}
}
return -1;
}
}

Hope this helps :). It will work at least :)


(bismillah)
(salam)

Jazakllah. Yes that helped a lot. It took some time to understand it all at once. I also found another type of linear search for numbers that I need a little help on. Here is the code and the output.

Posted Image


Notice in the output, it repeats "Not found" , four times as it reads through the nums array, except for when it reads the third index of the array, where 4 is located it prints "we found the number 4!". How do I get it to print ONLY "Not found" if 4 is not in the array or "We found the number 4" when it finds it in the array.

jazakallah

(wasalam)

Posted Image


#9 repenter

repenter

    Born With A Vengeance

  • Mods
  • 3,114 posts
  • Location:Beyn-ul-Harameyn

Posted 08 May 2013 - 09:18 AM

View PostPurifiedTruth, on 08 May 2013 - 03:13 AM, said:

(bismillah)
(salam)

Jazakllah. Yes that helped a lot. It took some time to understand it all at once. I also found another type of linear search for numbers that I need a little help on. Here is the code and the output.

Notice in the output, it repeats "Not found" , four times as it reads through the nums array, except for when it reads the third index of the array, where 4 is located it prints "we found the number 4!". How do I get it to print ONLY "Not found" if 4 is not in the array or "We found the number 4" when it finds it in the array.

jazakallah

(wasalam)


public static void main(String[] args)
{

boolean numberFound = false;


for(int i = 0; i<nums.length; i++)

{

if(nums[i] == 4)

{

System.out.println("We found the number 4!");

numberFound = true;

}

}


if(!numberFound)

{

System.out.println("Not Found");

}

}

Enjoy....

Edited by repenter, 08 May 2013 - 09:19 AM.


#10 sayedamir2000

sayedamir2000

    Member

  • Advanced Members
  • Pip
  • 137 posts
  • Religion:Islam

Posted 08 May 2013 - 02:44 PM

View Postrepenter, on 08 May 2013 - 09:18 AM, said:

public static void main(String[] args)
{

boolean numberFound = false;


for(int i = 0; i<nums.length; i++)

{

if(nums[i] == 4)

{

System.out.println("We found the number 4!");

numberFound = true;

}

}


if(!numberFound)

{

System.out.println("Not Found");

}

}

Enjoy....

(salam)

Brother PurifiedTruth the answer to your question is solution that brother repenter gave in above code.Try to understand it first on your own and if you don't understand it then let us know and we will help you to explain it. But one thing I wanted to clarify about for loops and why it actually printed four times "Not found". Reason is because in for loop you are always comparing specific element to your if clause, which is in this case comparison to number 4. If it is true then you print something out and if it not, then "Not found" is printed. This is done to every element in that order that you have placed your elements in your array.

#11 PurifiedTruth

PurifiedTruth

    خادم المهدي (Servant of our awaited Imam(AS))

  • Advanced Members
  • PipPip
  • 307 posts
  • Location:Canada
  • Religion:Quran and Ahlulbayt
  • Interests:Quran, Tafsir, Hadith, Theology, History, Astronomy , Anthropology, Engineering

Posted 09 May 2013 - 02:29 AM

View Postsayedamir2000, on 08 May 2013 - 02:44 PM, said:


(salam)

Brother PurifiedTruth the answer to your question is solution that brother repenter gave in above code.Try to understand it first on your own and if you don't understand it then let us know and we will help you to explain it. But one thing I wanted to clarify about for loops and why it actually printed four times "Not found". Reason is because in for loop you are always comparing specific element to your if clause, which is in this case comparison to number 4. If it is true then you print something out and if it not, then "Not found" is printed. This is done to every element in that order that you have placed your elements in your array.

View Postrepenter, on 08 May 2013 - 09:18 AM, said:

public static void main(String[] args)
{

boolean numberFound = false;


for(int i = 0; i<nums.length; i++)

{

if(nums[i] == 4)

{

System.out.println("We found the number 4!");

numberFound = true;

}

}


if(!numberFound)

{

System.out.println("Not Found");

}

}

Enjoy....


Jazakallah for both of your inputs. Much appreciated  I modified the code a bit as you can see. I also noticed that using the same code, I can make a fruit array and use the linearsearch method to search for a specific fruit by replacing the numbers with the fruit names.

Posted Image
As for now. I am done with linearsearch and hamdellah feel confident with it. Moving on to Binary search. I will post my questions if I need help on it.

Once again thank you both for your input. :)

(wasalam)

Posted Image


#12 repenter

repenter

    Born With A Vengeance

  • Mods
  • 3,114 posts
  • Location:Beyn-ul-Harameyn

Posted 09 May 2013 - 12:27 PM

I hope we aren't doing your homework for you.......

#13 sayedamir2000

sayedamir2000

    Member

  • Advanced Members
  • Pip
  • 137 posts
  • Religion:Islam

Posted 16 May 2013 - 04:31 AM

I hope as well



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users