0

Trying to help a student fix an assignment for his computer math class. The text is overlapping when he runs his applet. How do we change this?

The assignment is meant to teach basic if-then loops.

Here's the code:

import java.awt.*;
import java.lang.*;
import java.applet.*;
public class HibbertJ_if extends Applet {//more points lost
public void init() {   }
public void paint(Graphics g) {               
    int Rigby=(int)(Math.random()*123)+123;                      
    int Mario=(int)(Math.random()*321)+2;
    int SJF = (int)(Math.random()*2)+3;
    int Drake = (int)(Math.random()*9)+7;
    int Luigi = (int)(Math.random()*6)+8;
    int Benson = (int)(Math.random()*3)+4;
    boolean DoYouLikeToLose=getRandomBoolean();
    boolean DoYouLikeMario=getRandomBoolean();
    boolean DoYouSlackOff=getRandomBoolean();
    boolean IAmLolzingAtYou=getRandomBoolean();
    boolean IAmBeatingYou=getRandomBoolean();
    boolean DoYouLikeRegularShow=getRandomBoolean();
    String Go="Mr. Win";
    String Bball="Wii U";
    String Hib="PS4";
    String Son="3DS";
    String Marshall="Compaq";
    String Jordan="Beats";
    String student="sweeeeet";//need 3 string variables
    //type code here
    //start first word
    g.drawString("x = "+Rigby,100,15);
    g.drawString("y= "+SJF,200,15);
    g.drawString("Headaches= "+DoYouLikeMario,300,15);
    if(DoYouLikeMario)//equals NOT =, = assigns values, == says if same
    {
        if(SJF>=Rigby)//greater than or equal to
        {
            g.drawString("Mordo has a "+Rigby,10,15);

        }
        else
            g.drawString("SMB has a ",10,15);;
    }
    else
        g.drawString("SMG4 has a ",10,15);

    if(DoYouLikeMario)//equals NOT =, = assigns values, == says if same
    {
        g.drawString(Go,10,30);
        if(SJF>=Rigby)//greater than or equal to
            g.drawString(Bball,10,30);
    }
    if(Rigby<=SJF)//less than or equal to
        g.drawString(Hib,10,45);
    else if(SJF>Rigby)//greater than
        g.drawString(Son,10,60);
    else
    {
        g.drawString(student,10,75);
        if(Mario<SJF)//less than
        {
            g.drawString(Marshall,10,90);
            if(Rigby!=Rigby)//Not equal to
                g.drawString(student,10,105);
        }
    }
    if(Mario==SJF&&SJF>Rigby)//(equal to) AND (greater than)
        g.drawString(Jordan,10,120); 
      if(Luigi==Drake&&Benson>Rigby)//(equal to) AND (greater than)
        g.drawString(Bball,10,120); 
    if(Rigby==SJF||SJF==Mario)//(equal to) OR (equal to)
        g.drawString(student,10,135);
        if(Mario==Drake||Rigby==SJF)//(equal to) OR (equal to)
        g.drawString(Go,10,135);
    else
        g.drawString(Go,10,135);
    if(DoYouLikeMario)
        g.drawString(Bball,10,135);
    if(DoYouSlackOff)
        g.drawString(Hib,10,135);
    if(IAmLolzingAtYou)    
        g.drawString(Son,10,135);
    if(true)
        g.drawString("This always happens",1,100);
        //if(false); also works, ; means some line of code could be there
    }
   public boolean getRandomBoolean() {
       return Math.random() < 0.5;
   }
}
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Oct 30 '15 at 17:39

1 Answers1

0
if(Rigby==SJF||SJF==Mario)
    g.drawString(student,10,135);
    if(Mario==Drake||Rigby==SJF)
    g.drawString(Go,10,135);
else
    g.drawString(Go,10,135);

Blocks without curly braces are extremely error prone. In this specific case the compiler interprets it as:

if(Rigby==SJF||SJF==Mario) {
    g.drawString(student,10,135);
}

if(Mario==Drake||Rigby==SJF) {
    g.drawString(Go,10,135);
} else {
    g.drawString(Go,10,135);
}

Notice how it's very different from what the indentation implied? The best practice is to add curly braces on every if and else, even if they are single line. This avoids many future errors and has a very low cost.

Aside from that, drawing text by manually specifying position is hard to write and maintain. Try to use a layout manager, they are created specifically to ensure everything is in its place and there are not overlaps

BoppreH
  • 8,014
  • 4
  • 34
  • 71