javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The throw new MoviePropertiesNotSetException ("The movie properties are not set."); will be in the set methods. So when you create the movieNew object in main you will get an exception if the attributes are not set

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Before you add the argument h to the ArrayList do this:
If the age of the human is lower than 5 then throw a new IllegalAgeException


Call them in that order in the main method

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you click the button you need to hide the current JFrame and call the other.

class JFrame_A extends JFrame {
....
....

public void methodBuitonClicked( ...... ) {
  JFrame_B frameB = new JFrame_B();
  this.setVisisble(false);
  frameB.setVisisble(true);
}
.....
.....
}

If you want from the frameB to go back to A you need to pass the A as an argument at the B frame: JFrame_B frameB = new JFrame_B(this); Of course you will need to have the appropriate attribute at the JFrame_B.
Then set the B frame visible false and the frame A that you passed as argument to true

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of all the way you create your own exceptions is wrong. You don't need to create a String variables: exception.
The Exception class has a method: getMessage() which returns the description of the exception that occurred with everything you need to know. So you don't need the getException() method.
When you call super(msg); you set the message to be the String that you passed as argument, so when you call the getMessage() of your exception that argument will be returned:

public class IllegalAgeException extends Exception
{
    /**
     * Creates a new instance of <code>IllegalAgeException</code> without detail message.
     */
    //String exception;
    public IllegalAgeException() 
    {
         super();
         //exception="Unknown";
    }
    /**
     * Constructs an instance of <code>IllegalAgeException</code> with the specified detail message.
     * @param msg the detail message.
     */
    public IllegalAgeException(String msg) 
    {
        super(msg);
        //this.exception=msg;
    }
/*
    public String getException()
    {
        return this.exception;
    }
*/
}

How to use it:

Whenever something bad happens that it is wrong and you don't want it to happen call: throw new IllegalAgeException (); in a method. Then at the declararion of that method add: throws IllegalAgeException When you call that method, put it inside a try-catch and treat it as usual:

public void setAge(int age) throws IllegalAgeException {
  if (age<=0) throw new IllegalAgeException("Age: "+age+" must be positive");
  this.age = age;
}

AND

YourClass obj = new YourClass();
try {
  obj.setAge(10); 
} catch (IllegalAgeException iae) {
   System.out.println(iae.getMessage());
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

hi! can anyone please help me complete this code

You have got to be kiding

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Yeah I used proper coding for my powers and square roots, yet when I compile it still returns an error message saying i need a ; after fToC = (5 / 9)(f - 32);.

fToC = (5.0 / 9.0)*(f - 32)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You need to put commands such as this in methods: System.out.println("Input temperature in ºF ---> "); Also in java if you want the power of a number use: Math.pow(double a, double b)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

the variable q is not declared in the class A and I dont want to declare it. Is there anyway this can be done? I tried that and it says "non-static variable cannot be referenced from a static context" If also someone could share with me what this means. Thanks alot

BestJewSinceJC probably meant this:

C c = new C();
c.q;

That is how non static variables are accessed.
You can do the above anywhere you want (provided that q public)

And again, this can be done:
C c = new C();
c.q;
c.o;
c.p;


This NOT:

B b = new B();
b.q;

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

These calculation need to go inside the methods:

//The getArea method returns the value that is stored in the area field
	double area = (PI * radius * radius); //Formula to get the area of the circle
	public double getArea()
	{
		return area;
	}
	//The getDiameter method returns the value that is stored in the diameter field
	double diameter = (radius * 2); //Formula to get the diameter of the circle
	public double getDiameter()
	{
		return diameter;
	}
	//The getCircumference method returns the value that is stored in the circumference field
	double circumference = (2 * PI * radius); //Formula to get the circumference of the circle
	public double getCircumference()
	{
		return circumference;
	}

Try it this way:

public double getArea()
	{
		return (PI * radius * radius);
	}
	
        public double getDiameter()
	{
		return (radius * 2);
	}
	
        public double getCircumference()
	{
		return (2 * PI * radius);
	}

With the way you had it the variables were initialized when the object was created but then if you changed the radius, these values (area, diameter, ...) weren't changed.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Regardless of whether the elements have been separated by a split, regular expressions are the easiest way to provide a match pattern for the phone number that allows for the variability that was described.

Then follow Ezzaral suggestion if you are not familiar with none of the two recommendations

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

> And it worked. Although the file saved in my file system has extension .gif ??

The file name doesn't matter; its content does. I could have very well named it 'image.exe' and it still wouldn't have mattered. Maybe you overlooked the declaration File saveFile = new File("SemiTransparentSquare.gif"); in which the file name is pretty much a constant.

Oops

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You cannot because q is defined in C not in B

You cannot because it is declared private.

class A{
   private int o,p;
   protected B bClass;
   protected C cClass;

  public void method() {
     System.out.println(cClass.o+";"+cClass.p);

     //this is wrong: bClass.q
  }
}

Also none of the above will work because you define o,p,q to be private. They need to be public or have public get methods that return them.

Also:

C cObj=new C(1,2,3);
cObj.o
cObj.p
cObj.q

If they were public

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

That reference sheet has all the methods you will need as well as what they do and how they are called.

There is Math.round() that rounds a double:
10.9 --> 11.0
10.4 --> 10.0

If you want a certain number of decimal digits you can:

Multiply (for example) the number with 100, then round it with the above method and then divided again with 100:
10.123456 --> 1012.3456 --> 1012.00 --> 10.12

double d = Math.round(10.9);
System.out.println(d); //will print 11.0


double d = Math.round(10.4);
System.out.println(d); //will print 10.0
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The r.toString() returns a String, it does not print. You need to call System.out.println() with argument r or r.toString()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Try saving it to a .png instead of a .gif, that should (hopefully) work.

I changed in the code this: ImageIO.write(cp.bi, "gif", saveFile); into this: ImageIO.write(cp.bi, "png", saveFile); or ImageIO.write(cp.bi, "jpeg", saveFile); And it worked. Although the file saved in my file system has extension .gif ??

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Well for starters it always helps when debugging to add a ex.printStackTrace() in the catch:

System.out.print ("Caught a null pointer exception - ");
            System.out.println (ex.toString());   
            ex.printStackTrace();

After doing so, I noticed that none of your aruments is null. This is what I got:

java.lang.NullPointerException
at com.sun.imageio.plugins.common.PaletteBuilder.findPaletteEntry(PaletteBuilder.java:310)
at com.sun.imageio.plugins.common.PaletteBuilder.getIndexColorModel(PaletteBuilder.java:296)
at com.sun.imageio.plugins.common.PaletteBuilder.getIndexedImage(PaletteBuilder.java:145)
at com.sun.imageio.plugins.common.PaletteBuilder.createIndexedImage(PaletteBuilder.java:77)
at com.sun.imageio.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:564)
at com.sun.imageio.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:492)
at javax.imageio.ImageWriter.write(ImageWriter.java:598)
at javax.imageio.ImageIO.write(ImageIO.java:1479)
at javax.imageio.ImageIO.write(ImageIO.java:1521)

at ColorForms.TopPanel.SaveImageToFile(TopPanel.java:87)
at ColorForms.TopPanel.actionPerformed(TopPanel.java:46)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

I have never used these classes before so I am not sure how they need to be instantiated. I believe that maybe when the java methods are called
(from the stack trace:
at javax.imageio.ImageWriter.write(ImageWriter.java:598)
at javax.imageio.ImageIO.write(ImageIO.java:1479)
at javax.imageio.ImageIO.write(ImageIO.java:1521)
)
something that was not supposed to be is null when they use your arguments. I cannot say much so you would better wait for someone better to see this and figure it out

Sorry

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The error has nothing to do with the ArrayList. You forgot to implement the method: suggestions(String)

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Give me a minute to run the code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

if (cp.bi == null) What if cp is null ?

Try to print everything before you run anything:

File saveFile = new File("SemiTransparentSquare.gif");

System.out.println("saveFile: "+saveFile);

if (saveFile == null)
System.out.println ("saveFile is null");

System.out.println("cp: "+cp);
System.out.println("cp.bi: "+cp.bi);

if (cp.bi == null)
System.out.println ("cp.bi is null");
ImageIO.write(cp.bi, "gif", saveFile);
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Inside the run method of the Thread class have a boolean method take value true when it finishes. That variable will be declared as an attribute of the class.

When you call the thread have a while loop checking that variable when it becomes true you will exit the loop

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

From what I see they are comma separated, so use StringTokenizer or String.split() to separate into its elements and check each one of them if they are phone numbers or not

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

It will skip it if investments.size() is zero

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

When you have a degree as a parameter you don't know if it is in Celsius or in Fahrenheit. So you will use the scale to determine that and calculate the results accordingly.

So don't put any calculations in your get Methods.

When you set the degree (by Constructor or be a set Method), use the scale to see if it is Celsius or Fahrenheit and use the appropriate formula:

public void setDegrees(float degree)
{
  if (scale=='C') {
          celsiusTemp =degree;
          fahrenheitTemp = celsiusTemp * 5/9-32;
  } else {
         fahrenheitTemp =degree;
         celsiusTemp = fahrenheitTemp * 9/5+32;
  }
}

This is wrong: 'F' = scale;
This will work: scale = 'F';

Meaning that your set methods should have arguments and will pass that argument to the class variable.

Use the instructions of the assignment to create the 4 constructors. Based on the input calculate the fahrenheitTemp, celsiusTemp like I did in the setDegree. If no input specified the assignment tells you what values to put.

And you declared the same Constructor twice which gave you an error:

public Degree(float degree)
{
 temp = degree;
 scale = 'C';

}
 
public Degree(float degree)
{
 temp = degree;
 scale = 'F';

And the other mistakes are because of missing and/or misplaced brackets

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can you provide an example of what the lines might look like?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You mean this? :

public class A {
   public void method_A1 {
      
        //calling method from B
        newMethod_B1(); 
   }
}
public class B extends A {
   public void newMethod_B1() {
       
   }
}

No it cannot be done. What I wrote above is wrong. Although however you can do this, but has nothing to do with inheritance:

public class A {
   public void method_A1 {
      
        B b1 = new B();
        b1.newMethod_B1(); 
   }
}

You can create and call any class from anywhere, but like I said it has nothing to do with one class extending the other

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I observed that many programmers do not create separate functions.
They write the whole program in the main function.

What do you mean by saying: "many programmers". Because this is not the right way to do it. In any language. You must write small functions that are called in the main.
Most programmers that write large pieces of code in an efficient manner, write things in separate functions that are organized in different classes depending on their functionalities.
These functions are not necessarily used only once in main. They could be used by other different function more than once. So if you want to make a change, you only change the function. If you had most of the things in main then it would be very difficult to change or correct something.
Also you wouldn't have to repeat the same code all over again when you can only call one function

So the second approach is more preferable even in small programs

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Actually I just thought of a VERY DUMP way to do it. It is not smart from programming terms.
I have already told you a way that might be done (running the appropriate DOS command using java) but if it is too difficult you can always try this:

for (int i=0;i<100;i++) {
  System.out.println();
}

It doesn't do what you want but it will look like it

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You want to have 200 posts with the same problem everyday answering to the same post.....

No we will not have 200 post with the same question, because when you have a question the easiest thing to do is search the forum in case it has already been answered. That is the point of having a forum, so people will find the answer they are looking for. And if you can't find the answer then start a new thread. Otherwise we will be communicating with PMs and no one will ever post anything.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Hi Shubhang and welcome to DaniWeb :)

I am sorry, but I don't understand what you are asking. Can you please post some code to show us what you mean?

I think he is saying that when the application is running, in DOS for example, the window becomes too crowded with all the System.out.println messages. So he would like a way to "clear" the command window from the messages.

But I don't know if there is a way to do it purely with java. Of course there are DOS and UNIX commands that do that, so IF there isn't another way, you might want to try and calling those from your program. But this I think will get a little more complicated than that you actually wanted.

Of course if there is another way feel free to correct me.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

With the hashCode() you compare objects. It's no different than using equals()

So if you use: File.hashCode() or File.equals() you will compare 2 file objects. Now I don't know the rules that these methods were written but I presume that they compare the path of the file they refer to.
So by doing this:

File f1 = new File("fileName.txt");
File f2 = new File("fileName.txt");

Then equal will return true.
But:

File f1 = new File("fileName1.txt");
File f2 = new File("fileName2.txt");

I think that equal or hashCode will NOT return true, even if they have the same context. (but check it out anyway).


If you have 2 different files in the file system and want see they have the same context, then try this:
Use FileInputStream to read their bytes in an array and compare them

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
public class A {
   public void someMethod_A1() {

   }

   public void someMethod_A2() {

   }
}
public class B extends A {
   public void newMethod_B1() {
        //BOTH are the same:

         someMethod_A1(); //calls the method from super class
         super.someMethod_A1(); //exactly the same as the above
   }


// BUT if you also declare this:
    public void someMethod_A2() {

   }

//AND

    public void newMethod_B2() {
         this.someMethod_A2(); //calls the method from the class B (this class)

         super.someMethod_A2(); //calls the method from the class A (super class)
   }
}

I didn't quite understand what you are asking in your final post so I posted this example.
Let us know if you have any comments on it.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

There is Math.round() that rounds a double:
10.9 --> 11.0
10.4 --> 10.0

If you want a certain number of decimal digits you can:

Multiply (for example) the number with 100, then round it with the above method and then divided again with 100:
10.123456 --> 1012.3456 --> 1012.00 --> 10.12

Or :

java.text package
that has this:
DecimalFormat

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Can someone help me how to read and write text file using Java Code... ???

Thanks in advance..

Start you own thread. This thread started by someone who had a problem. Now, do you expect people to start helping you and stop answering the other guy's question?
Start your own thread and you will get plenty of answers and example code

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You cannot use what you declare in B inside A.
Only the other way around since it is B that extends A, so B can access what you declare in A

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

The Employee class has these declared private:

private String employeeName;
private String employeeNumber;
private String hireDate;


Since the ProductionWorker extends the Employee you don't need to declare them again in the ProductionWorker class. Declare only these variables:

private int shift;
private double payRate;

Since the first 3 in the Employee class are private use the get, set methods to have access to them. The constructor of ProductionWorker should have 5 arguments.
The first 3: String employeeName, String employeeNumber, String hireDate should be set using the set methods inherited by Employee and the other 2 directly to the shift and payRate.

Also the static variables should also be public and final. Since they will never change value no matter how many instances you create (the Night shift should always be 2 and the Day 1) you were right to have them static. But you need to make them final in order to ensure that nobody can change their values within the program. And of course public so others can use them as constants.


From the above suggestions you should be able to correct the Demo class.
Also the toString method of ProductionWorker should also print the employeeName, employeeNumber, hireDate. Use the get methods.

And you might want to check the argument of setEmployeeNumber(String num) . I think that you need to check if the format of the input is the one specified in the instructions: …

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

You can view a .class file like any other file: By opening it. Use whatever program you want to open it. But don't expect to see anything. Here is a sample of what a .class file looks like when I used PSPad:

CAFE BABE 0000 0031 0112 0A00 4E00 B409
002C 00B5 0900 2C00 B609 002C 00B7 0900
2C00 B809 002C 00B9 0900 2C00 BA09 002C
00BB 0900 2C00 BC09 002C 00BD 0A00 2C00
BE0A 002C 00BF 0A00 2C00 C00A 002C 00C1
0A00 2C00 C20A 002C 00C3 0A00 2C00 C40A
002C 00C5 0A00 2C00 C60A 002C 00C7 0700
C808 00C9 0A00 1500 CA08 00CB 0800 CC08
00CD 0800 CE0A 0027 00CF 0800 D00A 0027

As you can see it is binary code. Actually what you see is hex code (0,1,2,3,4,5,6,7,8,9,A,B, C,D,E,F) because this is how PSPad displays it, but in reality it is binary

Alex Edwards commented: I think he meant reverse-engineering, but that does answer the question! =) +4
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Also when you try to print the sentence in the end:
System.out.println (+text) (remove the '+' of course), the text variable has been modified in the while loop. So you will not print the original sentence. Save the input to one more variable and print that one in the end

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Or we can do your Homework since this is the purpose of this forum: Read this

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
BufferedWriter writer = new BufferedWriter(new FileWriter("filename.txt"));
writer.write("This is a line");
writer.newLine();


writer.close();

Also: BufferedWriter
FileWriter

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Write a separate method that takes a String as argument and removes all spaces:
Input: +0 69 34 55 --> Result: +0693455

And sometimes the first 0 is missing, and they appear as 868242602. and sometimes they might be in the form +353868242602.

Then another method that takes the above result and:
If it starts with: +353 remove it from the String
Then if 0 is missing added to the String.
Then check if the result is a number (long)
Then if it has length 10
Then if it starts with 08
Then if the 3rd character is [3,5,6,7,8]

And now you are sure that what you read is a number.

And use that ONE method to check every line in the file

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

First of reread the requirements for the Cinema class and do it all over again.

You will a Movie attribute in the class. You will not instantiate it but use the setMovie() to set its value.
When you call the showMovie, check if the above attribute is null or not in order to see if it set or not.
The same check when you add Humans.

Also when you add Humans all you do display if they are child or not. That is not what is required.
From the Human argument you will get the age. From the movie attribute (if it is set) you will get the rating. And from the MovieRating class you will check if you can add that human or not.

If the class needs a collection of Humans have an ArrayList as attribute and add the Human given to that ArrayList.

You don't need the isEmptyObject method. Just check if the movie is null or if their attributes are set (if they are null or not)

And again reread the requirements and see what you really need

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Haha that was a good one.

To allo me ton Toto to kseris?

The above is a greek expression used as a reply to someone who says or asks something stupid or impossible and he thinks that is normal to ask such thing

Do we have praful_engg's slaves written on our foreheads?

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Since there have been many changes to the code post the latest Cinema class.
I believe that the human class has a method getAge(). Use it to get the age, use the rating of the movie to get the minimum age and compare these two. You also have written a MRating class with a hashMap. You can use that.

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Post part of the code where you get the exceptions, as well as the printStackTrace()

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

i'm want to source code related to the project online airline ticket reservation in jsp 7 help to devlop in project

I guess your skills are limited only to writing and not to reading, because if you had read the previous posts you wouldn't have made such question thread-jacker

Also this is a 2 year-old post. What are doing posting in to this

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
if(Character.isDigit(s.charAt(j)))
		{

//x=(int)s.charAt(j);
// Try this and repeat to the other number y

char ch = s.charAt(j);
x = Integer.parseInt( String.valueOf(ch) );

                      
j++;
y=(int)s.charAt(j);
z= x + y;
System.out.print(z);
		}

After you take the char, if you cast to primitive int it will give you the ASCII. But you want the number.
So use: Integer.parseInt( ); But that method takes a String as argument. So you take the char, turn it into a String: String.valueOf(ch) And use the parseInt method:

Integer.parseInt( String.valueOf(ch) );

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Type cast Object to Integer:

Integer integ = (Integer)objectValue;
integ.intValue();

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

After reviewing the classes Family, I came to this conclusion.
You don't need an Adult class, nor a Child class. Family will not extend Human. It has a collection of humans.
This is how is done in reality. You family is not a Human. Your family consists by a group of Humans (father, mother, ...) they are all humans.

So you will have instances of Human class and you will use the age to determine if they are adults or not

Human h1 = new Human("name",5);
if (h1.getAge()<18) {
  System.out.println("This is a child");
}

I took the liberty of adding methods that do that check in the Human class. Also the Family will have an attribute ArrayList, in which you will add Humans (the members of the family)

Here is the modified code. Use these changes to correct the cinema class accordingly:

Human

public class Human {
    private String name = "";
    private int age = 0;

    public Human() {
    }
    
    public Human(String name, int age) {
        setAge(age);
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    public boolean isAdult() {
        return (age>=18);
    }
    
    public boolean isChild() {
        return (age<18);
    }
    
    public String toString() {
        return "Name: "+name+", Age: "+age;
    }

and Family

import java.util.*;
import java.io.*;
//import java.util.Iterator;
public class Family
{
    private ArrayList<Human> familyMembers;
    private …
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

Are Adult and Child instances of the Human class?:

Human adult = new Human();
Human child = new Human();

ArrayList<Human> list = new ArrayList<Human>();
list.add(adult);
list.add(child);

adult, child are instances of the Human class


OR

Assume you have a class: Human.
And 2 subclasses: Child extends Human and Adult extends Human

Then both classes are Human so you can do this:

Adult adult = new Adult();
Child child = new Child();

ArrayList<Human> list = new ArrayList<Human>();
list.add(adult);
list.add(child);

But remember, in the 2nd case, when you do get from the list you get back Human instances. And unless you know if what you get back is Adult or Child then you can only use methods that are declared in the Human class.

Human h1 = list.get(0);
h1.callMethod(); //declared in the Human class
Human h2 = list.get(1);
h2.callMethod(); //declared in the Human class

Child ch1 = (Child)list.get(1);
ch1.callSomeMethod(); //declared in the Child class

If the list.get(1) is not Child but Adult then you will get a Runtime error.

So before doing this: Child ch1 = (Child)list.get(1)
check if the returned object is Child: Google for: "instance of" keyword

Of course the second case might not be what you need to implement, I haven't read the code.
Also:

ArrayList

javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

that's okay,
How do I check that the movie property has been set and if not throw an exception in the addMovieGoers() method?

Use 'if' and 'throw' statements