hi im having a hard time fixing and identifying the problem. sorry abstraction really confuses me. the problem is I should write a weather prediction program using the random number generator. three outcomes should be possible: rain sun and cloud.

import java.util.Random;
public abstract class WeatherPredicMain 
{
        public static void main(String [] args)
        {

    public String Rain;
    public String Sun;
    public String Cloud;
    public String Random;



        public WeatherPredicMain()
        {   
        }
        public void setRain(String Rain)
        {
            this.Rain=Rain;

        }
        public void setSun(String Sun)
        {   
            this.Sun=Sun;
        }
        public void setCloud(String Cloud)
        {
            this.Cloud=Cloud;
        }
        public String getRain()
        {
            return Rain;
        }
        public String getSun()
        {   
            return Sun;
        }
        public String getCloud()
        {   
            return Cloud;
        }
        public String getRandom()
        {
            int rand;
            int num;
            String outcome=" ";

            //random generator
            Random t= new Random();

            for(rand=1; rand<=3;rand++)
            {   
                num=t.nextInt(3)+1;


            //random allocator
                for(int x=0;x<3;x++)
                {
                if(num==x)
                    outcome=Rain+" ";


                if(num==x)  
                    outcome=Sun+" ";

                if(num==x)
                    outcome=Cloud+" ";
                }
                }

            return outcome;
        }
    }

}




public class Weather extends WeatherPredicMain
{
    //public static void main(String [] args)
    {
        WeatherPredicMain outcome = new WeatherPredicMain();

        outcome.setRain("Rain");
        outcome.setSun("Sun");
        outcome.setCloud("Cloud");

        System.out.println(outcome.getRain());
        System.out.println(outcome.getSun());
        System.out.println(outcome.getRandom());
    }   

Edited 2 Years Ago by hahahanz: revis

... this code makes no sense.
you have no abstract methods whatsover, and you don't implement them in child classes. in the code above, I have no idea why you even have a child class.

actually, on a second glance: it's even worse. your main problem has nothing to do with abstraction, rather with you not knowing the basics. you are declaring everything in your abstract method within the main method.

you can't do that. defining methods within methods will make your compiler act like a drugged junk trying to solve a rubics cube. you'll be lucky to have a cube left when he's "done".

stop working on abstract classes, start on the basics (how and where to define variables, methods, ....) and what the consequences are.
for instance: the scope of a variable declared within a method are very different from the scope of a variable declared on class level.

just an example of an abstract class:

public abstract class MyAbstract{
  public void printRandom(); // no method body.
}

and here's the implementing child class.

public class MyConcrete extends MyAbstract{

}

compiling the class like this will fail, since "MyConcrete is not abstract, and does not implement all abstract methods of MyAbstract"

public class MyConcreate extends MyAbstract{
    public void printRandom(){
        System.out.println("random");
    }
}
This article has been dead for over six months. Start a new discussion instead.