hiddepolen 32 Posting Whiz in Training

If numberfilms is 2, there was 1 film, and the second film (which is in films[1]), is the added film. You can't ask for films[2], because there is nothing in there.

film [1] is the added film, so you should only check if the film is equal to the previous films. If you check if the film is equal to itsself, it will always return true.

hiddepolen 32 Posting Whiz in Training

The numberFilm-1 is needed, because the last entry in the films array is the added film. The function will always return true that way, because it will find itsself.

Think yourself about the return, and what you do with it. What does the true or false from the method mean (Duplicate or not?)?

When the user enters a duplicate, he should just return to the menu, and be able to exit or reenter a film.

hiddepolen 32 Posting Whiz in Training

Great! If the thread is solved, please do so!

hiddepolen 32 Posting Whiz in Training

What I would do is just call --numberFilms. The duplicate film is still there, but the next added film will overwrite the duplicate.

hiddepolen 32 Posting Whiz in Training

You ARE only checking the last two.

- loop starts
- first check, x=1, i=0
- not equal, boolean is false
- second check, x=2, i=1
- duplicate found! boolean is true
- third check x=3, i=2
- not equal, boolean is false
- loop ends
- return, false, even though there was one duplicate

If you really only want to check the duplicate addition, loop through the array, and compare the current object:

public static boolean checkFilm(Film[] callFilms){
		Film addedFilm = callFilms [numberFilms-1];
		boolean check = false;
		for(int i = 0; i < numberFilm-1; i++, x++){ // note the numberFilm-1!
if (callFilms[i].equals(addedFilm)) // I am using .equals()
check = true; // if there is any match, the boolean is true. Else, don't do anything
		}
		return check;
	}
hiddepolen 32 Posting Whiz in Training
public static boolean checkFilm(Film[] callFilms){
		int x = 1;
		boolean check = true;
		for(int i = 0; i < numberFilm; i++, x++){
			if(callFilms[x] == callFilms[i])
				check = true;
			else
				check = false;
		}
		return check;
	}

I see what you are trying to do here, but you should use the .equals() method. Two different objects, even with the same data will never return true if you compare them using ==. The reason for this is that you are comparing the locations of the two objects, not the objects themselves.

Also, the program is going to crash because of an ArrayOutOfBoundsException. callFilms[x], when x is always 1 bigger then i: x will be numberFilms when i is numberFilms-1. This will crash, because the max of callFilms is numberFilms-1. Add a check next to i < numberFilm for x < numberFilms.

What I don't understand, is that you only compare the last two entries in the array, and return that value. Do you want to:
- Only check the last two?
- Check if any films match?

If you only want to check the last two, why loop through the entire array? Else, why do you update the same variable on every run of the loop?

hiddepolen 32 Posting Whiz in Training

I think it would help if you let the LinkedBinaryTree class implement Iterable<E>.
Then, you should be able to iterate in a for loop.

hiddepolen 32 Posting Whiz in Training

Was the actual assignment to use an array? Please write down the exact assignment, so that we can help you better.

Anyway, it is still possible to have only one array, and keeping a static int with the number of films in the array. You can loop through that, like I did in my code.

hiddepolen 32 Posting Whiz in Training

but as my lecturer said, if the varible doesn't initialize also is equal to 0;

It is true that the value is 0, but you cannot use it. It is also wrong not to initialize it, because of the readability of your code. Just always initialize a variable, even with 0. Else, the program will not compile or there will be errors later on.

i cannot do in one array, since the assignment question require us to define an array with size 100, so i use numberFilm to detect how many films they have, so it won't looping for 100 times instead of looping the value of numberFilm.

Yes, you can:

public class ClientProgram {
    Film[] films = new Film[100];
    static int numberFilm;
// ...

public void someMethodWithForLoop () {
for (int i=0; i<numberFilm; i++) {
doSomethingWithTheFilm (films[i]);
}
}
public void addFilm(Film f) {
//...
if (numberFilms < 100) {
films[numberFilms++] = f; // I add one to the number of films, AFTER the addition of the film to the array.
}
else {
System.out.println("Error, too many films");
}
//...
}
}

Easier would be:

public class ClientProgram {
    ArrayList<Film> films= new ArrayList<Film>();
// ...

public void someMethodWithForLoop () {
for (int i=0; i<films.size(); i++) {
doSomethingWithTheFilm (films.get(i));
}
}
// or, even better: 
public void someMethodWithForLoop () {
for (Film f: films) {
doSomethingWithTheFilm (f);
}
}
public void addFilm(Film f) {
//...
films.add(f);
//...
}
}

ArrayLists are underestimated, they are very useful, because …

hiddepolen 32 Posting Whiz in Training

Did you initialize the numberFilms variable? That might cause a problem.

hiddepolen 32 Posting Whiz in Training

What I can see from the picture, is that you need to clear your screen on a repaint().

Two ideas:
- Call super.paint(g); in the first line of your paint method. This is the paint method of the super class, it might help.
- Call a clearRect(0,0,getWidth(), getHeight());. This clears the screen, and might do the job.

Let me know if it works.

hiddepolen 32 Posting Whiz in Training

Also, why do you need two arrays (

Film[] films = new Film[100];
    static int numberFilm;
    static Film[] callFilms = new Film[numberFilm];

). I think someone has already said that: it doesn't make sense. Could you maybe write down the exact assignment, so we don't have to keep quessing what you are doing?

hiddepolen 32 Posting Whiz in Training

But draw the triangle (0,0)(1,0)(2,0)! Ofcourse the area is 0, because we have three points in one line.

Use (0,0), (2,3) and (4,1) to check.

cecsFTL commented: Very helpful! +1
hiddepolen 32 Posting Whiz in Training

What are a, b and c then in the method I gave you?
If you use it like this, it should work...

public boolean isValid () {
double a = getSideAB();
double b = getSideBC();
double c = getSideCA();
return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
// return false if any of the statements are true, else return false.
}
hiddepolen 32 Posting Whiz in Training

Our method returns invalid on what set of points?

hiddepolen 32 Posting Whiz in Training

I don't actually think so, there are so many checks that should not be needed.

- Get all the three sides, A, B and C.
- if (a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0), return false
- else, return true.

Like the example above:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
// return false if any of the statements are true, else return false.
}
hiddepolen 32 Posting Whiz in Training

If it works, please mark the thread as solved!

hiddepolen 32 Posting Whiz in Training

I think it would help to start programming classes with Capital Letters. This makes reading the code so much easier:

Film[] films = new Film[100];
	static int numberFilm = 0;
	Film[] arrFilms = new Film[numberFilm];
//...
new Film();

instead of:

film[] films = new film[100];
	static int numberFilm = 0;
	film[] arrFilms = new film[numberFilm];
//...
new film();
hiddepolen 32 Posting Whiz in Training

Please mark as solved!

hiddepolen 32 Posting Whiz in Training

I think you messed up the position variable in the button handler a bit. I changed the code to this, and it works:

public void actionPerformed(ActionEvent event) {
		if (event.getSource () == nextButton) {
			if (position < productArray.length - 1)
				position++;
			else
				position = 0;
		} else if (event.getSource () == firstButton) {
			position = 0;
		} else if (event.getSource () == lastButton) {
			position = productArray.length - 1;
		} else if (event.getSource () == prevButton) {
			if (position > 0)
				position--;
			else
				position = productArray.length - 1;
		}
		displayProduct ();

	}
hiddepolen 32 Posting Whiz in Training

Yes, I agree to that too :).

hiddepolen 32 Posting Whiz in Training

I think it would be better to return null instead of "". That way, the rest of the methods can check easier if there was any 'real' return.

xiangzhuang commented: thansk for the reply +0
hiddepolen 32 Posting Whiz in Training

No. What we are now talking about are global and local variables:

You can declare a variable in two ways: global and local. Global means inside a class, but outside anyethod. This variable can be used anywhere in this class, and is object dependent.
You can also declare a variable locally, this means INside a method or block. This variable will ONLY exist as long as the method or block runs.

Example:

class A {
int a; // global variable
static int b; // global static variable
public static void main {
int c; // local variable for method main
static int d; // local static variable for method main
}

void doSomething() {
while (something) {
int e; // local variable in while block
static int f; // local static variable in block (makes no sense)
}
}
}
hiddepolen 32 Posting Whiz in Training

Ah, yes, I was thinking of something entirely different.
My suggestion then, should solve all the problems:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b || a == 0 || b == 0 || c == 0);
// return false if any of the statements are true, else return false.
}
hiddepolen 32 Posting Whiz in Training

This works for me: simple spacing with '\t'.

class Test {
	
	public static void main(String[] args) {
		System.out.println ("a\tbc\tdef\t.");
		System.out.println ("\t\t\t.");
	}
}
hiddepolen 32 Posting Whiz in Training

True, getArea() should call getPerimeter(), getPerimeter() should call all the getSides().

What is your definition then of a triangle? Because I think we must both agree that we need three points, and two points (B and C) in the same spot are not two different points. I wouldn't consider that a triangle at all...

hiddepolen 32 Posting Whiz in Training

Using static:

I think you were confused by static methods. Mainly you will get compile errors on using variables in the main method (where any program starts). The main method is static, and you cannot use any variables you use in your class because of that. Make an instance of the class you want to use, and you can use your normal variables:

class A {
public static void main (String [] args) {
new A();
}
A () {
doWhatEverYouWantWithYourNonStaticVariables();
}
}

The '\t' character is the best for spacing out outputs. Use more then one to make large spaces, with some testing it should work out. You can also work with spaces and someString.length and while loops to fill up spaces, but I think that is quite ugly.


Please mark this thread as solved when it is solved.

hiddepolen 32 Posting Whiz in Training

Well, it uses the perimeter variable, but it does not call the getPerimeter() method. So, Taywin is right to wonder. Place a System.out.println(); call in the getPerimeter method, and check if it is called.

hiddepolen 32 Posting Whiz in Training

OK, that is a good example. Personally I have never found Wikipedia wrong, but I only use it for easy defenitions like a triangle...

So, I hope you agree with my method then:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b);
// return false if any of the statements are true, else return false.
}
hiddepolen 32 Posting Whiz in Training

Nice program :)

The problem is the use of static. When you use static, the value of the variable is class dependent, and not object dependent.

Example:

class A {
A(int a) {
this.a = a;
}
public static int a; // Note: STATIC
}

class B {
void doSomething () {
A.a = 1;
// A.a = 1
A.a = 5;
// A.a = 5

// Note: I never created any object of A. Because of the static flag, I can just use the class.

new A(6);
// A.a  = 6
}
}
class A {
A(int a) {
this.a = a;
}
public int a; // Note: NOT STATIC
}

class B {
void doSomething () {
A a1 = new A(1);
// a1.a = 1
A a2 = new A(2);
// a1.a = 1 and a2.a = 2

// Note: Each object keeps is own values, because a is now object dependent.
}
}

Good luck!

hiddepolen 32 Posting Whiz in Training

@Taywin
I think we need to discuss here what we difine as a triangle. I have looked it up on Wikipedia (yes, I do find that reliable information):

A triangle is one of the basic shapes of geometry: a polygon with three corners or vertices and three sides or edges which are line segments. A triangle with vertices A, B, and C is denoted ABC.
In Euclidean geometry any three non-collinear points determine a unique triangle and a unique plane (i.e. a two-dimensional Euclidean space).

A triangle made with the defenition above CAN have an area of 0. All it needs is a 2D space (we have got it: only two coordinates), and three points (check!).
So I think the triangle (0,0) (1,0) (2,0) should be valid.

My method will return that as valid, yours will not. My conclusion is that my two functions are correct.

(Doesn't really matter: I don't think the user will ever enter three points in one line, what is more important: what does the assignment say?

hiddepolen 32 Posting Whiz in Training

Any three points in a 2D system should be valid. (0,0) (2,3) (4,4) is a valid triangle.
Think of some invalid points: they don't exsist, or I haven't found any :)...

hiddepolen 32 Posting Whiz in Training

One question from my side: Do you want to save 1 digit after the comma, or you you want to remove the last digit of the number?

Assuming you want to remove one digit:
- Make a string out of the double, adding
- Copy the string, but without the last character
- Copy another string, with only the last character of the first string.
- Check if the value of the single last digit is 5 or higher, and set a boolean
- If the boolean is false, use the value of the string of the second step.
- Otherwise, use the value string, and add 1 to the last digit of the double.
- Print out the generated number.

There are other options ofcourse, but this is one I have used once, it worked.

hiddepolen 32 Posting Whiz in Training

No. You must check exactly what I said: if ANY of the following are true, the triangle is INVALID: a+b<c, a+c<b and b+c<a (where a, b and c are the sides);

In code:

public boolean isValid () {
return !(a+b<c || b+c<a || a+c<b);
// return false if any of the statements are true, else return false.
}

Or:

public boolean isValid () {
return (a+b>=c && b+c>=a && a+c<=b);
// return true if all the statements are true, else return false.
}

In the future think about what you want the function to do, what statements you need, what those statements mean, and what you should return using the calculations you made in the function.

hiddepolen 32 Posting Whiz in Training

If AB + BC == AC, the points are on a like like you say. Would you call that a triangle? I wouldn't.

I'm sorry to tell you that this is wrong again. You MUST check for ab+bc>ac and all other sides as well. If you are going to check the opposite (using less than), you must use "<=" or you will get it wrong. In other words, regardless the way you check, you MUST check all 3 sides.

That is exactly what I am saying. If any of the following is true, the triangle is INvalid: a+b<c, a+c<b and b+c<a.

hiddepolen 32 Posting Whiz in Training

It may seem stupid, but I learned all my Java from the internet. I used YouTube to follow tutorials, and found out the rest myself (just looking on Google). Maybe a thing to try, and see if you like it :).

hiddepolen 32 Posting Whiz in Training

Taywin is right here. You don't want to check is a+b > c, that is valid for any triangle. You should check for a+b < c. Make sure that your isValid method includes: a+b < c, a+c < b and b+c < a. If any of them are true, you have an invalud triangle.

hiddepolen 32 Posting Whiz in Training

I know it will not compile, but as an example, what would happen IF you wouldnt handle any exceptions.

I hope it is a little more clear.

hiddepolen 32 Posting Whiz in Training

OK, good. Now, when I try your program: you have no handling for all the errors that come up when I place ships in impossible positions. Do that first, think of every possible error that can pop up, and then go on to registering the hits of the computer and the human.

hiddepolen 32 Posting Whiz in Training

I think you don't understand the concept of static, and exceptions. Those things are VERY different.

Static:
When you make a method or variable static, you call it not using a variable, but using the class it is part of.
In your case: You did not make any instance of your main class, and you just call your methods from that class.
My example:

class Test {
	
	public static void main(String[] args) {
		new Test ();
	}
	
	Test () {
		B.sayHiFromB ();
		A objectA = new A();
		objectA.sayHiFromA ();
	}
	
	private class A {
		public void sayHiFromA () {
			System.out.println ("Hi from A");
		}
	}
	private static class B {
		public static void sayHiFromB () {
			System.out.println ("Hi from B");
		}
	}
}

will output:

Hi from B
Hi from A

As you can see, I have to make an object from the A class to call the method. I don't have to make a B object, I can call it straight from the B class.

Exceptions:
An Exception is an error. Certain methods throw Exceptions, this means that an error can exsist in such a method. You should use a try/catch block surrounding the method call, to catch it:

class Test {
	
	public static void main(String[] args) {
		new Test ();
	}
	
	Test () {
		A objectA = new A();
		try {
			objectA.sayHiFromA ();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println ("Program exits.");
	}
	
	private class A …
hiddepolen 32 Posting Whiz in Training

Please, talk a little nicer and a little more civilised. I know you can do it...

There is no ; at the end of your line... Can't you come up with that yourself?

hiddepolen 32 Posting Whiz in Training
#include<stdio.h>
main()
{
   char date_of_birth, day, month, year[15]
       printf("Enter your date of birth:");
#include<stdio.h>
int main()
{
   char date_of_birth, day, month, year[15]
       printf("Enter your date of birth:");

Find the difference...

hiddepolen 32 Posting Whiz in Training

What program are you using to program in Java (this is called your IDE, Integrated development environment).
In most programs, you have to right-click in the bar with all the line numbers, and select: 'add breakpoint'. When you build and run your program, the program will run until it encounters this line of code where you put your breakpoint. It will stop there, and when it has stopped you can hold your mouse over any variables in your code, and read their current values.
Try it, you will understand it in no time.

Is there any reason you are making all your variables and methods static?
It seems a little strange to me.

What exactly does not work in the methods you mentioned? Do they return a wrong value, does the program crash, are there any errors?

hiddepolen 32 Posting Whiz in Training

Look at my previous post, tip #2.
- Use int main(){} instead of main (){}.

hiddepolen 32 Posting Whiz in Training

I am just trying to help you man, try to find out some things for yourself!

Some tips:
- Dont use spaces in names (like the person above me said)
- Use int main () {} instead of main() {}

hiddepolen 32 Posting Whiz in Training

Super!

Now, when I try to build this code in my project, I get all these errors:

1>------ Build started: Project: TestProject, Configuration: Debug Win32 ------
1>Build started 18-10-2011 13:42:43.
1>PrepareForBuild:
1> Creating directory "D:\Documenten\C++\TestProject\Debug\".
1>InitializeBuildStatus:
1> Creating "Debug\TestProject.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> Main.cpp
1>d:\documenten\c++\testproject\testproject\main.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2146: syntax error : missing ';' before identifier 'of'
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2065: 'of' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2146: syntax error : missing ';' before identifier 'birth'
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2065: 'birth' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2065: 'Day' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2065: 'Month' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(4): error C2065: 'Year' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(6): error C2146: syntax error : missing ')' before identifier 'of'
1>d:\documenten\c++\testproject\testproject\main.cpp(6): error C2059: syntax error : ')'
1>d:\documenten\c++\testproject\testproject\main.cpp(8): error C2065: 'Day' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(10): error C2065: 'Month' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(12): error C2065: 'Year' : undeclared identifier
1>d:\documenten\c++\testproject\testproject\main.cpp(13): error C2146: syntax error : missing ')' before identifier 'of'
1>d:\documenten\c++\testproject\testproject\main.cpp(13): error C2059: syntax error : ')'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.88
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Also I see this in my window (I use Visual Studio 2010 Ultimate): See picture.

hiddepolen 32 Posting Whiz in Training

OK, but I am not the person to trace your mistakes and correct them, YOU have to do that. I can only help you to understand HOW to correct them. I just gave you tips on how to do that in my first post.

Maybe you're not begging me to do your homework, but it comes close.

Code tags are put using the big button in the top of the editing screen, saying CODE (hard to make that up?).

Do you have any specific questions, about errors you are getting, the types of problems you run into, or one problem that needs explaining? I will help you.

hiddepolen 32 Posting Whiz in Training

Please use Capital Letters. Also don't ask anyone to do your homework.

If you have any specific question, or errors you do not understand, don't hesitate to ask, I'd love to help you!

hiddepolen 32 Posting Whiz in Training

And it's due tomorrow too! BUMMER.
Please! Please! Please! S.O.S
p/s: I'm waiting patiently for a reply thread here ASAP. Thanks in advance.

Please use code tags, and don't beg people for doing your homework...

As you say, there are 6 errors. What do these errors say? Often there are line numbers, so look in those lines, and solve the error.

If you have any specific question, I'd love to help you!

hiddepolen 32 Posting Whiz in Training

Please use normal sentences, and capital letters.

Do you have Eclipse as your IDE? Even when you don't, you should debug. Make a breakpoint at the method you want to analyze, and check all your variables when the program is running at that point.
Most likely some method will show something wrong.

Also, please post the relevant parts of your code, not all of it.

When you found errors, ask here, and I (or someone else) will try to answer you.