Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 14, 2007, 04:00 PM   #1
happyElephant
macrumors member
 
Join Date: Oct 2006
Beginning algorithms...

Has anybody heard/used the book"Beginning algorithms" by S.Harris&J.Ross?
What do you think about it? Would you advise it for the very beginner?
I've been trying to get an idea of JUnit test&code it tests: I've created two files(JUnit test&corresponding code-both examples from the textbook) in Net Beans Blue J5.0(Xcode is still quite tough to handle.
JUnit test results were:"Failed to prepare tests". I wonder why?
Neither I could execute the code because it doesn't have "main "method(I've also unsuccessfully tried to squeeze "main" in):

package com.wrox.algorithms.iteration; // what kind of package is this?
public final class PowerCalculator{


public static final PowerCalculator INSTANCE=new PowerCalculator();
private PowerCalculator(){

}
public int calculate(int base, int exponent){
assert exponent>=0:"exponent can't be<0";
int result=1;
for(int i=0;i<exponent;++i){
result*=base;
}
return result;
}

}

Summarising it all, I have my exams in a week(loads of maths, mecanics, computer architecture, networks(html,php),signal processing...algorithms). As a result, I decided to skip JUnit testing(in our algorithm class they didn't talk about it) for a while & learn some basic functional codes(algorithms) only.
Please, tell me where to add "main" in order to make this "PowerCalculator" function(I'll do the same with other codes(ex.sorting etc.)


P.S.: Sorry, if sometimes it looks that I have no idea what I'm talking about-it could be the case
happyElephant is offline   0 Reply With Quote
Old Jan 14, 2007, 05:58 PM   #2
Llywelyn
macrumors member
 
Join Date: Oct 2003
Quote:
Originally Posted by happyElephant View Post
Has anybody heard/used the book"Beginning algorithms" by S.Harris&J.Ross?
What do you think about it? Would you advise it for the very beginner?
I've been trying to get an idea of JUnit test&code it tests: I've created two files(JUnit test&corresponding code-both examples from the textbook) in Net Beans Blue J5.0(Xcode is still quite tough to handle.
JUnit test results were:"Failed to prepare tests". I wonder why?
Neither I could execute the code because it doesn't have "main "method(I've also unsuccessfully tried to squeeze "main" in):

package com.wrox.algorithms.iteration; // what kind of package is this?
public final class PowerCalculator{


public static final PowerCalculator INSTANCE=new PowerCalculator();
private PowerCalculator(){

}
public int calculate(int base, int exponent){
assert exponent>=0:"exponent can't be<0";
int result=1;
for(int i=0;i<exponent;++i){
result*=base;
}
return result;
}

}

Summarising it all, I have my exams in a week(loads of maths, mecanics, computer architecture, networks(html,php),signal processing...algorithms). As a result, I decided to skip JUnit testing(in our algorithm class they didn't talk about it) for a while & learn some basic functional codes(algorithms) only.
Please, tell me where to add "main" in order to make this "PowerCalculator" function(I'll do the same with other codes(ex.sorting etc.)


P.S.: Sorry, if sometimes it looks that I have no idea what I'm talking about-it could be the case
First, JUnit is a unit testing framework for Java that derives from smalltalk. Unit testing is a type of test that goes after just about anything you can think of programmatically for your class, generally testing its contract. In "test driven development" the tests are written (or at least designed) before the class is ever written.

In Java, it is fairly common to write a unit test to provide a main function for testing the class that does not have one. Eclipse will create the files for you and let you set up the main function.

Second, though this isn't in your questions, use a getInstance() accessor for a singleton. There are three basic paradigms of singleton creation to be aware of.

1) Static initialization. This is similar to what you are doing, but the instance variable is private and there is a public getInstance() method.

2) Lazy initialization. Set the instance variable to null and synchronize the getInstance call. In the getInstance, check to see if the instance variable is null and, if it is, initialize it.

3) A SingletonHolder pattern. This is a kind of lazy initialization where you statically initialize the public instance inside of a private inner class. Then getInstance() returns SingletonHolder.instance.

To more directly answer your question. Working in Java, it needs a main function.


This, for instance, is what Eclipse generates for me:
Code:
package test;

/**
 * @author llywelyn
 *
 */
public class Demonstration {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Hello, World!");

	}

}
So, if you stick a main function (following this same pattern) in your Java class, it will let you run that class directly.

Some stylistic comments, that may not relate directly to what you are doing (or may be contradicted by the homework assignment):

* The method should have a nice, easy, memorable, and distinctive name. "calculate" tells me nothing about what it does, "exp" or "exponent" does.
* Exponents can be negative. Simply take the reciprocal.
* Normally if an argument is invalid you don't want to assert it. You want to throw an IllegalArgumentException.
* I assume you can't use Math.exp?

Hope this helps, let me know if you have any further questions.

Post script: Why are you doing this in Java?

Last edited by Llywelyn; Jan 14, 2007 at 06:11 PM.
Llywelyn is offline   0 Reply With Quote
Old Jan 17, 2007, 07:54 PM   #3
happyElephant
Thread Starter
macrumors member
 
Join Date: Oct 2006
Quote:
Originally Posted by Llywelyn View Post


To more directly answer your question. Working in Java, it needs a main function.


This, for instance, is what Eclipse generates for me:
Code:
package test;

/**
 * @author llywelyn
 *
 */
public class Demonstration {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Hello, World!");

	}

}
So, if you stick a main function (following this same pattern) in your Java class, it will let you run that class directly.

Some stylistic comments, that may not relate directly to what you are doing (or may be contradicted by the homework assignment):

* The method should have a nice, easy, memorable, and distinctive name. "calculate" tells me nothing about what it does, "exp" or "exponent" does.
* Exponents can be negative. Simply take the reciprocal.
* Normally if an argument is invalid you don't want to assert it. You want to throw an IllegalArgumentException.
* I assume you can't use Math.exp?

Hope this helps, let me know if you have any further questions.

Post script: Why are you doing this in Java?
Thank you for this information.

I know that working in java requires "main". However I'm not sure about where to place it in this case...
Basically, I've learned everything, I know about java by now, on my own. Even though we have a java class, unfortunatelly, our prof is hardly available, "shortcutting" students who have questions
The same scene during computer network class. Here we face "especially constructive atmosphere" for ex.: I missed some 16hrs of the course because of my trip to Asia. When I returned, our prof asked me "to make a question-answer sheet" in html&php, knowing that I haven't ever heard about it before(he wanted to take his revenge on me for missed classes ).
Pure luck or what, but I was able to do it in less than an hour(catch up). As a result he promissed "to screw" me during the exam...
One more ex: I sent my math homework to my prof written with the help of LaTeXiT. The remark from him was:"Are you trying to show off here with your fancy presentation?!". Atmosphere is "very motivating"one would say...
In the conclusion, whatever I learn about programming, software installation(or how much I motivate myself) etc. is either from the books or with you in this forum.
Thankful to everybody for their help


P.S.: I would be happy to ask much more questions if somebody has too much of free time&doesn't mind answering them for me.
happyElephant is offline   0 Reply With Quote
Old Jan 18, 2007, 05:56 PM   #4
Llywelyn
macrumors member
 
Join Date: Oct 2003
Quote:
Originally Posted by happyElephant View Post
Thank you for this information.

I know that working in java requires "main". However I'm not sure about where to place it in this case...
Anywhere in the class you are trying to execute. I like to put it at the bottom as a nice, standard, location. Is that what you are looking for?

Quote:

P.S.: I would be happy to ask much more questions if somebody has too much of free time&doesn't mind answering them for me.
Sure, go for it. In the meantime, have you considered a different school?
Llywelyn is offline   0 Reply With Quote
Old Jan 19, 2007, 11:49 AM   #5
happyElephant
Thread Starter
macrumors member
 
Join Date: Oct 2006
Thanks Llywelyn
Actually, I'm in Master's program. Sure, I'm considering further studies in UK or US.


P.S.: This is just to give you a little idea about how things function in general in France:
http://www.frenchgardening.com/postc...56820165499524

This has nothing to do with gardening It is about experiencing French culture
happyElephant is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Apple Bought Matcha for Its Content Recommendation Algorithms MacRumors MacRumors.com News Discussion 44 Aug 19, 2013 02:57 PM
Beginning to despise ML- tigres OS X 10.8 Mountain Lion 4 Apr 9, 2013 04:47 AM
It's beginning . . . elberto1 iOS 6 9 Nov 5, 2012 04:06 PM
Sandy, only the beginning? Ugg Politics, Religion, Social Issues 33 Nov 3, 2012 11:38 AM
Beginning C++ on a mac feanorkinslayer Mac Programming 12 Jul 19, 2012 09:51 AM

Forum Jump

All times are GMT -5. The time now is 07:09 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC