Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

1365281

Suspended
Original poster
Oct 27, 2006
82
0
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:)
 

Llywelyn

macrumors member
Oct 27, 2003
48
0
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?
 

1365281

Suspended
Original poster
Oct 27, 2006
82
0
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.
 

Llywelyn

macrumors member
Oct 27, 2003
48
0
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?

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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.