Win a copy of Event Streams in Action this week in the Java in General forum!

Quazi Irfan

Ranch Hand
+ Follow
since Jan 29, 2015
Quazi likes ...
Chrome IntelliJ IDE
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
9
Received in last 30 days
0
Total given
81
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Quazi Irfan

You code won't compile because the return statement has error in it. Also if you want to execute code you need to have an entry point, which is main function declared as public static void main(String... arg).

Also, I am not sure why you got the error message. You need to set you 'Run/Debug configuration' settings in IntellijIDEA.

Instead of using a feature rich editor like IntellijIDEA, I think it's easier to start learning using just the command line a simple text editor. Here is a collection of Java tutorials from Oracle. They starts at the very basic: https://docs.oracle.com/javase/tutorial/?sess=16e492aba137894101940f7f88d9f51f
1 year ago

Manishii yadav wrote:Hi

Can anyone share the link where lambda expression nicely explained?

Thanks



I've also struggled with finding a good article/document that explains lambda in a way that I understood. But this article finally did it: http://java.amitph.com/2012/08/at-first-sight-with-closures-in-java.html
1 year ago

Campbell Ritchie wrote:But there is a more efficient way to total those numbers.



Can you tell me more about it?
1 year ago

Campbell Ritchie wrote:How do you know you will want loops? Isn't that jumping ahead a bit?



Most people would repeatedly add numbers to get a sum of a range. I am assuming he knows that much. And I am instructing him to repeat the same tasks in a programming language.

Is that what you are asking?
1 year ago

Anton Plotnikov wrote:i want write this function.
what code do I need to insert?



Okay, lets work together.

Before we jump start writing the code lets look at the steps we need to take to solve the problem. You can write these steps in many different way: flow chart, pseudo code etc. I like pseudo code because it's more similar to written description. You can also add code in a pseudo code for brevity which I find very handy. For example, I can write a := 0 which means, variable a is set to value zero. Here is the pseudo code for GetSum function,

// start of function GetSum
Function GetSum(Number1, Number2):
   summation := 0
   In a loop
       summation := summation + Number1
       Increment Number1
       Check if Number1 == Number2
             True: break the loop
             False: continue the loop
   return summation
// end of function GetSum

Different language provides different ways to write this function. For example, Java has three different loop constructs: For, while, do-while. Which one do you think would work best for our situation? You also know need to know how to use variables since you need to store the result of the summation somewhere. You would also need to know how to use use arithmetic operator to modify the value stored in a variable.

Once you read those tutorials try writing the function. Post here if you are stuck or have trouble reading the java tutorials. (Note that this function doesn't do exactly what you want, it just gives you a starting point.)

         
       
       
1 year ago

Omas Sakau wrote:



In your first loop, in 6 seconds red, yellow and green lights are turned on and off. You need the same process to repeat for 150 seconds. How many loops you need for it to continue for 150 seconds?

Also, look into for-loop tutorials on Java. You can modify the loop variable inside the body of the loop, it's much simpler to do it inside the for-loop syntax.
1 year ago

Stephan van Hulst wrote:No, it wouldn't. If you leave @FunctionalInterface and remove @Override, you get this error message:



Right, sorry I meant if the clone() is the only method in the functional interface.
1 year ago

Campbell Ritchie wrote:As you will see, that becomes a compiler error because of the access modifier.
Did you read the JLS llink from Friday? The interface doesn't have an implicit clone() method to override.



Yes I did it. But I think you don't show an implementation of the interface to answer my question. The simplest answer IMO is that unlike 'public String toString()', 'protected native Object clone()' is not visible in an interface and therefore @Override annotation can not resolve what method is being overridden. The functional interface would work if the annotation is removed.
1 year ago
I am a bit confused. You said "Because of the lower visibility, it is regarded as a syntax error." But you also said "interfaces don't have clone() and finalize() methods implicitly". How come you lower the visibility of a method you do not have?

1 year ago

Stephan van Hulst wrote:You can not lower visibility of a method.



Because of changing the visibility, IDE is considering it as a non-overridden method, right?
1 year ago
I am learning the use of stream API on Java collections by following this blog: http://java.amitph.com/2012/10/java-collections-api-enhancements.html

I've stumbled upon this piece of code that I can't seem to get to work,


I am trying to reproduce the behavior with the following code,


But, IntellijIDEA says that it can not resolve method 'into'. I looked into Sream class, and I could not find a method named 'into'.

So, how do I make the code work? The blog is from 2012 - did the API change?
1 year ago
Ok, I think I've got it - Methods in Object class have different relationship with interfaces.

In the 1st code: Multiple abstract methods are allowed only if other methods are public methods from Object class. That's why the first code with toString() works.

In the 2nd code: clone() is not a public method of Object, therefore IntellijIDEA think it's a second abstract method in a functional interface, which is not allowed.

Am I right?
1 year ago
The following functional interface works since toString is implemented by Object class already.

But the following code does not. I am wondering why. Is it because the implementation of clone method is not included in the body of Object class?

At the location of @Override, IDE reports error saying "Method does not override method from it's superclass". And at the location of @FunctionalInterface, IDE reports error saying "Multiple non-overriding abstract methods found in interface functionalIterface"

1 year ago