aspose file tools*
The moose likes Beginning Java and the fly likes Can someone send me any kind of examples; trying to learn java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone send me any kind of examples; trying to learn java" Watch "Can someone send me any kind of examples; trying to learn java" New topic
Author

Can someone send me any kind of examples; trying to learn java

william chaple
Ranch Hand

Joined: Mar 08, 2013
Posts: 90
Hi I am getting hooked on programming and was wondering if you guys can send completed working samples with no errors

Things I am learning

- arrays
- polymorphism
- recursive
- array list with extended classes and/or polymorphism
- recursive with arrays
-if and while loops



I tried looking for java files but most of them have errors or not completed. Please guys any help or examples would be great so I can practice on my own
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome William. First of all, are you new to Java language? Which book are you studying from?


~ Mansukh
william chaple
Ranch Hand

Joined: Mar 08, 2013
Posts: 90
Mansukhdeep Thind wrote:Welcome William. First of all, are you new to Java language? Which book are you studying from?




Hi thank you very much. Well I am somewhat new.

Like i know the basic instance variable

return types
instance methods (might need a resfresh ment)

loops to pass through an array and I just study from the internet.

for instance I saw this





package Temperature.Scanner;


import java.util.Scanner;

public class Temp_converter_Scanner {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

int temp;
int F;
int C;

System.out.println("What would you like to convert?");
System.out.println("eneter number");
Scanner myScanner = new Scanner(System.in);
temp = myScanner.nextInt();

F = temp*9 / 5 + 32;
C = (temp - 32) * 5 /9;

System.out.println("F -----> C = " + C);
System.out.println("C--------> F = " + F);


}
}




i did this and now I am trying to figure out how to use scanner.in by asking what you would like to convert. I am thinking I have to use case switch?

Like i just input the number, but I want it to ask me step by step

"what do you want to convert" "type F for farenheight or c for celcius"

[input letter]

"enter number"
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Well, lets organize things a bit here Will so that you learn the things in the right manner and it is fun. Also, you need to keep certain things in mind when asking questions to gain the most out of the experts:



a) When you have a doubt regarding some java code, always use code tags. It makes the code much more clear to understand. Have a look:



Isn't it neater?

b) Secondly, no one is going to provide you with ready made working code samples. That goes against the very idea of having such a forum. We are here to help you. But you have to make the effort to learn things on your own. Start by studying the book I recommended(c). Write down what you don't understand, what are your doubts etc.. Then frame it and post. We are always here for you.

c) Buy a copy of the "Head First Java" book by Kathy-Bert. Devote at least an hour to two daily studying it. Don't go learning randomly on the internet. That's an approach I would not recommend. Build a strong base of fundamentals first. Your concepts should be rock solid. For example, when you asked

i did this and now I am trying to figure out how to use scanner.in by asking what you would like to convert. I am thinking I have to use case switch?


There is nothing called Scanner.in as far as I know. Perhaps what you were trying to ask is "How to ask the user for input using Scanner class?" Correct? So, unless you know the terminologies and concepts clearly, you would end up framing questions incorrectly.

Answer to your query: This piece of code is simply asking the user to provide the input using the standard input stream (System.in) which is scanned using a Scanner class reference. Then , it finds the scanned value using the nextInt() method and performs the respective calculation. You can go ahead and use switch - case construct for choosing which conversion factor to apply depending on what the user enters.
william chaple
Ranch Hand

Joined: Mar 08, 2013
Posts: 90
how did you add that clip to add the code?

oh yes, I mean system.in, not scanner.in

I do have to learn the termenoligies
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

When you click on reply, at the top there appears a menu bar which has all the options. Click on "Code". You will get it. Understood?
william chaple
Ranch Hand

Joined: Mar 08, 2013
Posts: 90






oh thats so awesome! I never seen that before!

anyhow, do they have that book on ebay?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

The first step would be to add the code that prints the question. compile and get that to work. don't worry about GETTING the input, just have it print the question. Make sure it works. Test it about 200 times.

THEN add the code that actually gets the f or c. Don't DO anything with it yet...just get it. ok...MAYBE you could at the same time put in code to echo it back out, as in "you entered :f:". and i would literally sandwich it between two colons, or two dashes, or something, so that i was sure what EXACTLY I had gotten. Then test that about 200 times.

then, once I have it I would put in code that does nothing more than says "you want to convert F to C" or "You want to convert C to F". don't DO the conversions yet. Test it about 200 times.

...

So hopefully you see a pattern here. I never never never write more than a few lines of code at at time before I recompile and re-test. I never never never try and do too much at once. I never try and do two things at the same time, if I can do them one at at time. TRUST me - this is the best advice you can get, learn, and take to heart. It is simply not possible to spend too much time compiling and testing. The more often you compile and test, the easier you will find programming to be.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

anyhow, do they have that book on ebay?

Sure. Search for it. You will get it. And like Fred said, start with smaller code snippets first. JUST GET THEM WORKING. That should be your first aim. It could be simple addition , printing Strings etc. Once you start getting a hang of things, then we can move on to the more complicated stuff.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

fred rosenberger wrote:I never never never try and do too much at once. I never try and do two things at the same time, if I can do them one at at time.


Hear, hear!

I got bit by this recently, and bit hard.

I had several different tasks for the upcoming release. One of them touched a whole bunch of files, all over the app and our in-house libs that it uses. While I was working on that one, in a few cases I'd be in a file that I knew I had to modify for one of the other tasks. The right thing to do would have been to finish the big task, check everything in, then start new change lists with separate check-ins for the other smaller tasks. But no, even with I-know-better-becuase-I've-learned-the-hard-way-over-20-years lessons under my belt, I had to be in a hurry and take a shortcut and make all the changes at the same time, "since I'm here anyway."

When we released the app, the big change had a bug in it that flooded our servers with authorization retries, and since we get tens of thousands of phones updating on the first day we release a new version, it slammed us hard and fast, and probably killed the user experience for everybody. So, fine, roll back the changes, re-release, and the early adopters from the bad version will quickly upgrade to the clean on. But because I did too much at once, I had to roll back everything. I couldn't just roll back the bad stuff. All the other nice little features had to get rolled back too. Then I had to go back and remember how I'd done all those other changes so I could redo them from scratch a second time.

So, yes, no matter how long you do this, and no matter how good you think you are, write, compile, test, debug, checkin in small, independent pieces, or wish that you had.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

william chaple wrote:
- arrays
- polymorphism
- recursive
- array list with extended classes and/or polymorphism
- recursive with arrays
-if and while loops

please define them. then I can spend more time than you to give example which you cant find in search engine like yahoo, AOL, yes Google
william chaple
Ranch Hand

Joined: Mar 08, 2013
Posts: 90
Seetharaman Venkatasamy wrote:
william chaple wrote:
- arrays
- polymorphism
- recursive
- array list with extended classes and/or polymorphism
- recursive with arrays
-if and while loops

please define them. then I can spend more time than you to give example which you cant find in search engine like yahoo, AOL, yes Google



thank you =)

Well, as for now, I am just working on a simple calculation, which I got and understand. However, I am now trying to get familiar with system.in. I am pretty much using java.util.Scanner so the user can input what he wants to calculate. this is my code so far




its working, I just want the user to input what he/she wants to calculate rather then having both display.


example

"Type f to calculate for farenheight or type C for celcius"

[user inputs F and it will just calculate farenheight]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're asking the user, "What would you like to convert? C for Celcius or F farenheight?" (spelling: Fahrenheit), and prompting for a C or an F, but then you're calling nextInt(). Do C and F look like ints? Look at the docs for java.util.Scanner(⇐click) and see if you can find a method that's more appropriate to what you're trying to do.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

I would also suggest that while you're working on the whole C or F issue, you comment out the part that does a calculation. Don't delete it, as there is stuff there you will use later. But for now, in a way, it is just cluttering up what you are trying to focus on.

Also, something to think about LATER...

You got a little lucky with your conversion. If you had written it this way:

you'd be pretty surprised with your results each time...
william chaple
Ranch Hand

Joined: Mar 08, 2013
Posts: 90
Jeff Verdegan wrote:You're asking the user, "What would you like to convert? C for Celcius or F farenheight?" (spelling: Fahrenheit), and prompting for a C or an F, but then you're calling nextInt(). Do C and F look like ints? Look at the docs for java.util.Scanner(⇐click) and see if you can find a method that's more appropriate to what you're trying to do.



I am a bit confused.

isnt the output going to be returned as an int since its calculating? I was imagining doing case switch.

so, I need to find a method that will return based on what the user puts? F and C? and after that?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

william chaple wrote:
I am a bit confused.


Look at these two lines:


What, exactly, do you think the first line does?

What do you expect the user to do after reading that line?

What, exactly, do you think the second line does?

Do you see now why there will be a problem?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

william chaple wrote:I am a bit confused.

That is most likely because you are not thinking through it enough. Everybody starts off thinking that writing code is 90% writing code, and 10% thinking. That is backwards. I probably spend 90% of my time thinking and 10% writing code.

so...

THINK through the steps. What has to be done in what order? I will often literally write it out in English...

1) Ask the user which way they want to convert
2) get the user's input
3) Ask the user for a number that they want converted
4) get the user's input
5) Do the conversion.


Each and every one of these can be done independently of the others. Even this masks a lot of what needs to be done. for example, #4 could be broken down further

4a) Get the user input
4b) convert from a string to an integer type
4c) Validate the input (you can't convert -600 since that is below absolute 0)

etc. only code one at a time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone send me any kind of examples; trying to learn java
 
Similar Threads
How to send text Message to Phone using java
Whether Overloading is a way to implement Polymorphism?
Defining object oriented terms
How to send text Message to Phone using java
knapsack, towers of hanoi, v. stuck!