4 Replies - 2675 Views - Last Post: 08 February 2009 - 12:32 PM Rate Topic: -----

#1 tscott  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 370
  • Joined: 30-January 09

Problem Solving in Java 1st class I've taken

Post icon  Posted 08 February 2009 - 10:46 AM

Hi everyone I'm in my first Java class this semester and I seem to struggle the most with the problem solving my math skills are decent but it seems the hardest part is figuring out how to go at the problem. Are there any books or sites out there that could improve my problem solving skills in regards to programming?

Any help is greatly appreciated
Is This A Good Question/Topic? 0
  • +

Replies To: Problem Solving in Java 1st class I've taken

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Problem Solving in Java 1st class I've taken

Posted 08 February 2009 - 10:50 AM

I recommend checking out this ebook: it has lots of programming exercises and sample quizzes, in addition it has lots of explanations for the problems and the concepts teached in each chapter: http://math.hws.edu/javanotes/
hope this helps, good luck ;)
Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Problem Solving in Java 1st class I've taken

Posted 08 February 2009 - 10:54 AM

if you want to improve your programming skills then write lots and lots and lots of code.. the only solution is to practice A LOT..first make sure that you understand the basic concepts very very well..if you understand everything perfectly then you just need to experiment and practice..also ask a lot of questions like: why would this give me an error?..are there any more ways to write this?, how can i make my code more efficient?.. also take care of the good coding practices(indentation, refactoring, conventions, etc)... good luck
Was This Post Helpful? 0
  • +
  • -

#4 sdo  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 06-February 09

Re: Problem Solving in Java 1st class I've taken

Posted 08 February 2009 - 11:45 AM

Programming is a relatively low level exercise.

I'm sure your instructor has made a point of talking about how computers can't make assumptions or cut corners like the human mind can; they need to be told what to do EXACTLY or they will fail.

Having said that, there are really only a few mechanisms you have to solve any problem in programming. That is, the problem is always solved by very few, simple actions. Some people will now say "but what about programs like full scale applications which can be hundreds of pages of code?" Well, those applications aren't solving one problem, they are combined to create a synergy that solves MANY MANY MANY problems. Always remember that a "problem" in computer science must be broken into its smallest possible parts, and then solved individually. This will ALWAYS come down to one of a few simple things:

A ) You will solve it with a few lines of code operating on some data
B ) You will solve it in a few lines of code in a loop (or a nested loop) operating on some data
C ) You will solve it with either A or B and then operate on some data in a data structure like an array/vector/some other container

If you want to become a better programmer, just remember that programming is not about complexity.. it is about REDUCING complexity until the problem is simple. Once that is done, solve each part of it (which will be easy, since it is simple) and then combine those parts to make an awesome program that others will look at and marvel at its complexity (you would be surprised how complex a collection of simple things can be).

Since I'm on a role here, I'll give an example from another topic I just helped with. Consider this problem: Take user input (a sentence) and count the number of times letters recur. Then, output the result to the screen.

Ok, there are a few problems here:

1) how do we get the user input?
- i) how do we allow the user to input info?
- ii) how do we make that info a sentence?
2) how do we count the letters?
3) how do we output the result?

Ok, first number 1:

Scanner kbd = new Scanner(System.in);
		String userInput = kbd.nextLine();

We use a Scanner to get the input.. then we use the "nextLine()" function to obtain a full sentence! This was solved using method A (a few lines of code)

Number 2:

		for (int i = 0; i < 26; i++){
			for (int k = 0; k < userIn.length(); k++){
				if (userInput.charAt(k) == alpha.charAt(i))
					current++;
			}

We use nested for loops to iterate over the inputted sentence, and check for each individual character (and all the characters were stored the the data structure of a String, more technically known as a character array). This means we used method B + C (because the data is in an array).

Finally, part 3:

System.out.println("The most frequently recurring letter is '" + highestChar + "' with " + highest + " occurrences.");

We use System.out.println() to print what we found to the screen. This is method A again, only a single line of code.

Finally, lets put it in a main method, declare all our variables, and stick the whole thign together:

import java.util.Scanner;

public class charFinder {
	public static void main(String[] args) {
		
		int current = 0;
		int highest = 0;
		char highestChar = 'a';
		
		String alpha = "abcdefghijklmnopqrstuvwxyz";
		
		Scanner kbd = new Scanner(System.in);
		String userIn = kbd.nextLine();
		
		for (int i = 0; i < 26; i++){
			for (int k = 0; k < userIn.length(); k++){
				if (userIn.charAt(k) == alpha.charAt(i))
					current++;
			}
			if (current > highest){
				highest = current;
				highestChar = alpha.charAt(i);
			}
			current = 0;
		}
		System.out.println("The most frequently recurring letter is '" + highestChar + "' with " + highest + " occurrences.");
	}
}


Voila! A complete and working program that fully solves the original problem, which was reasonably difficult, BUT, by breaking it up, it became a series of easy problems.

Now, I know what you're thinking as a beginner: "Those problems weren't easy! I have no idea what that stuff is/means!" Well, look at it this way, you could have googled any of those issues (e.g. issue 3, google: "Java how to output to console or screen") and you could have easily figured it out. Besides, you will learn all this stuff in class.

So, going back to my original point: if you want to be a better programmer (or just a better problem solver overall), learn to compartmentalize, turn big problems into collections of little problems, and then solve them when they are split up and defenseless!

Hope this helped!

SDO

This post has been edited by sdo: 08 February 2009 - 11:52 AM

Was This Post Helpful? 1
  • +
  • -

#5 slikz  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 03-February 09

Re: Problem Solving in Java 1st class I've taken

Posted 08 February 2009 - 12:32 PM

To echo what SDO said, assume nothing and compartmentalize. These were the two most important concepts I learned when starting out.

Another thing I like to do when I have a difficult time wrapping my head around a problem is to plan out on paper (or text editor) what the flow of the program is. Usually I will construct a skeleton with blank methods, classes and any variables I think I will need. Then under those methods or classes I will usually write in plain English what I think should be happening there. This helps with breaking down the problem into manageable chunks. Only after that is done will I write actual code and start testing. Don't try and write the source code from scratch if you don't understand what you're supposed to be writing in the first place; you'll just end up staring at the screen. This might seem like overkill if you're only writing code in main with one class, but it really helps once you start having multiple methods and classes.

Personally, I would recommend picking some book and sticking with it until you have some fundamentals. I tried site hopping when I first started out, but it wasn't until I got myself a book that I grasped the fundamentals and was able to code without staring at references all of the time. Then I was able to look at independent examples and make sense of them.

Another free book here. I haven't read this particular one, but I do have the 4th edition and it's quite good.

Practice on your own time as well. I know if I only relied on the information that my profs gave me, I'd be sunk already.

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1