This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheHDMICord
TheHDMICord wrote ...

2020/9/10

Need help with WBC-4 chapter 5 Greenfoot Homework!

TheHDMICord TheHDMICord

2020/9/10

#
Hi, I'm taking a Java course right now, and my homework requires me to build upon WBC-4 by adding getters and setters for numOfBacteriaTouched, numOfBacteriaMissed, numOfVirusTouched, and numOfVirusMissed. Here's the exact instructions of the part I am stuck on: How to increase any of the above variables by one? Hint: first, you can use the public getter to obtain the current value of the variable, and then increase this value by one, and then use the public setter to set this increased value back to the variable. And that is why we provide each variable with a public setter and getter in step 1 above. In order to call the public getter and setter, you need to first obtain an instance (handler) of the class Bloodstream. So far, I have implemented the setters and getters, and have successfully implemented incrementing for numOfVirusTouched and numOfBacteriaTouched. However, the issue I've been pulling my hair out over for the last two hours is that I can't seem to correctly implement incrementing for numOfBacteriaMissed and numOfVirusMissed. Here's the code for my bloodstream, my virus and my bacteria:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The bloodstream is the setting for our White Blood Cell scenario. 
 * It's a place where blood cells, bacteria and viruses float around.
 * 
 * @author Michael Kölling
 * @version 0.1
 */
public class Bloodstream extends World
{
    public int score;
    private int time;
    private int numberOfBacteriaTouched;
    private int numberOfBacteriaMissed;
    private int numberOfVirusTouched;
    private int numberOfVirusMissed;
    
    /**
     * Constructor: Set up the starting objects.
     */
    public Bloodstream()
    {    
        super(780, 360, 1); 
        setPaintOrder(Border.class);
        score = 0;
        numberOfBacteriaTouched = 0;
        numberOfBacteriaMissed = 0;
        numberOfVirusTouched = 0;
        numberOfVirusMissed = 0;
        prepare();
        showScore();
        time = 2000;
        showTime();
        countTime();
        showBacteriaTouched();
        showBacteriaMissed();
        showVirusTouched();
        showVirusMissed();
    }

    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Bacteria(), 779, Greenfoot.getRandomNumber(360));
        }
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 359);
        }
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Lining(), 779, 1);
        }
        if (Greenfoot.getRandomNumber(100) < 1)
        {
            addObject(new Virus(), 779, Greenfoot.getRandomNumber(360));
        }
        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new RedCell(), 779, Greenfoot.getRandomNumber(360));
        }
        
        countTime();
    }
    
    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        WhiteCell whitecell = new WhiteCell();
        addObject(whitecell, 105, 179);
        Lining lining = new Lining();
        addObject(lining, 126, 1);
        Lining lining2 = new Lining();
        addObject(lining2, 342, 5);
        Lining lining3 = new Lining();
        addObject(lining3, 589, 2);
        Lining lining4 = new Lining();
        addObject(lining4, 695, 5);
        Lining lining5 = new Lining();
        addObject(lining5, 114, 359);
        Lining lining6 = new Lining();
        addObject(lining6, 295, 353);
        Lining lining7 = new Lining();
        addObject(lining7, 480, 358);
        Lining lining8 = new Lining();
        addObject(lining8, 596, 359);
        Lining lining9 = new Lining();
        addObject(lining9, 740, 354);
        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border, 770, 180);
    }
    
    public void showScore()
    {
        showText("Score: " + score, 80, 25);
    }
    
    public void addScore(int points) 
    {
        score = score + points;
        showScore();
        
        if (score < 0) 
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }
    
    private void showTime() 
    {
        showText("Time: " + time, 700, 15);
    }
    
    private void countTime() 
    {
        time = time - 1;
        showTime();
        
        if (time < 1) 
        {
            Greenfoot.stop();
            showEndMessage();
        }
    }
    
    private void showEndMessage() 
    {
        showText("Time is up- you win! \nYour final score: " + score, 360, 180);
    }
    
    public int getNumberOfBacteriaTouched() 
    {
        return numberOfBacteriaTouched;
    }
    
    public int getNumberOfBacteriaMissed() 
    {
        return numberOfBacteriaMissed;
    }
    
    public int getNumberOfVirusTouched() 
    {
        return numberOfVirusTouched;
    }
    
    public int getNumberOfVirusMissed() 
    {
        return numberOfVirusMissed;
    }
    
    public void setNumberOfBacteriaTouched(int numberOfBacteriaTouched) 
    {
        this.numberOfBacteriaTouched = numberOfBacteriaTouched;
    }
    
    public void setNumberOfBacteriaMissed(int numberOfBacteriaMissed) 
    {
        this.numberOfBacteriaMissed = numberOfBacteriaMissed;
    }
    
    public void setNumberOfVirusTouched(int numberOfVirusTouched) 
    {
        this.numberOfVirusTouched = numberOfVirusTouched;
    }
    
    public void setNumberOfVirusMissed(int numberOfVirusMissed) 
    {
        this.numberOfVirusMissed = numberOfVirusMissed;
    }
    
    public void showBacteriaTouched() 
    {
        showText("Bacteria Touched: " + numberOfBacteriaTouched, 100, 290);
    }
    
    public void showBacteriaMissed() 
    {
        showText("Bacteria Missed: " + numberOfBacteriaMissed, 92, 310);
    }
    
    public void showVirusTouched() 
    {
        showText("Virus Touched: " + numberOfVirusTouched, 88, 330);
    }
    
    public void showVirusMissed() 
    {
        showText("Virus Missed: " + numberOfVirusMissed, 80, 350);
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Bacteria fload along in the bloodstream. They are bad. Best to destroy
 * them if you can.
 * 
 * @author Michael Kölling
 * @version 0.1
 */
public class Bacteria extends Actor
{
    private int numberOfBacteriaMissed;
    
    /**
     * Constructor. Nothing to do so far.
     */
    public Bacteria()
    {
    }

    /**
     * Float along the bloodstream, slowly rotating.
     */
    public void act() 
    {
        setLocation(getX()-Greenfoot.getRandomNumber(3)-2, getY());
        turn(1);
        checkBacteriaMiss();
    }
    
    private void checkBacteriaMiss() 
    {
        if (getX() == 0) 
        {
            getWorld().removeObject(this);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.addScore(-15);
            bloodstream.setNumberOfBacteriaMissed(bloodstream.getNumberOfBacteriaMissed() + 1);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Virus here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Virus extends Actor
{
    private int numberOfVirusMissed = 0;
    
    /**
     * Act - do whatever the Virus wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX()-4, getY());
        turn(-1);
        checkVirusMiss();
    }
    
    private void checkVirusMiss() 
    {
        if (getX() == 0) 
        {
            getWorld().removeObject(this);
            Bloodstream bloodstream = (Bloodstream)getWorld();
            bloodstream.getNumberOfVirusMissed();
            numberOfVirusMissed += 1;
            bloodstream.setNumberOfVirusMissed(numberOfVirusMissed);
            bloodstream.showVirusMissed();
        }
    }
}
When I run the program, the game plays fine and I can even increment the numOfBacteriaTouched and numOfVirusTouched as normal, but as soon as a virus or bacteria touches the edge, the program stops and returns the error "Java.lang.NullPointerException" as well as highlighting the setter lines for both my virus and bacteria classes. I'm genuinely at a loss for what to do and I don't think I'll ever solve this without outside assistance, so I'm pleading for someone to help me with this ASAP! Thanks in advance!
danpost danpost

2020/9/10

#
In your Virus class, switch lines 28 and 29. The virus cannot get the world if it is not in the world. Same with lines 35 and 36 in the Bacteria class. Line 30 in the Virus class is a no-op function call. You have a value returned, but it goes nowhere. It needs to be put into a variable or used immediately (within the same line of code). To be sure, the "get" method returns the value of the field, not the field itself.
TheHDMICord TheHDMICord

2020/9/10

#
That seems to have fixed the issue! Thank you so much danpost, you're an absolute lifesaver!
You need to login to post a reply.