Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

comp sci intro course question https://www.dropbox.com/sh/oyyicwn4xgx3m26/AACeqj

ID: 3679338 • Letter: C

Question

comp sci intro course question

https://www.dropbox.com/sh/oyyicwn4xgx3m26/AACeqjFTcBEP585x301Li-vta?dl=0

Implement the Text Box class which will provide both input boxes into which the user can type input, and output boxes which simply display information. a.This class should have one extra instance variable: a boolean value indicating whether or not this is an input box that can process characters typed by the user, or an output box that only displays text. b.A constructor with parameters (x c, y c, hw, h h, t xt, i n p) where the first four arc the usual specifications for the rectangular area, txt is the text to be displayed in the box, highlighted is false, and inp is a boolean value indicating whether or not this is an input box. c.Write a draw() method which will use the superclass draw method to draw the rectangular outline of this box, and then display the text inside it. The text should be left-justified, starting a small distance from the left side of the box. d.Write a handleClick(double x, double y) method. As usual, the superclass handleClick method will tell you whether or not the click is inside this box, and whether you should return true or false. If the click is inside this box, and if it is an input box, then the click should clear the existing text from the box (make it an empty string) and set highlighted to true (meaning the box is now ready to accept input). If it isn't an input box, then clicks will be ignored. e.Write a handleCharTyped(char c) method. This is the only type of object that can handle a typed character. If the box is an input box, and it is currently highlighted, then it should handle the character as follows: If c is not the RETURN character (' '), then append the typed character to the end of the text in the box and redraw it. If c is the RETURN charactcr, set highlighted to false. In either ease, return true since it has now handled the charactcr. If it is not an input box, it should return false and ignore the character. f.Finally, write a display Text (String s) method which will changc the text in the box to s, and redraw it.

Explanation / Answer

import java.util.Scanner;

public class TextBox1 extends GUIelement {

  
private static final double MARGIN = 0.005;

  
public TextBox1(double xc, double yc, double hw, double hh, String txt, boolean inp = handleClick(xc,yc)) {
super(xc,yc,hw,hh,txt,inp);
}

public void draw(){
StdDraw.setPenRadius();
super.draw();
StdDraw.textLeft(xc+hw+MARGIN,yc,txt);
if(highlighted) {
  
StdDraw.line(xc-hw,yc+hh, xc+hw,yc-hh);
StdDraw.line(xc-hw,yc-hh, xc+hw,yc+hh);
}
}

public boolean handleClick(double x, double y) {
if(!super.handleClick(x,y))
return false;
else {
highlighted = !highlighted;
draw();
return true;
}
}

}

public class TestA3Q1 {
   public static void main(String[] args) {
   final double xcentre = 0.2;
   final double ycentre = 0.6;
   System.out.print("Enter String ");
   Scanner text = new Scanner(System.in);
   final double TEXTBOX_WIDTH = 0.38;
final double TEXTBOX_HEIGHT = 0.05;
   GUIgroup allElements = new GUIgroup();
   allElements.addElement();
   allElements.addElement(new TextBox1(xcentre,ycentre,TEXTBOX_WIDTH,TEXTBOX_HEIGH,text));
   TextBox outbox;
allElements.addElement(outbox = new TextBox1(xcentre,ycentre,TEXTBOX_WIDTH,TEXTBOX_HEIGH,text));
allElements.draw();
int count = 0;
while(true){
if(StdDraw.hasNextKeyTyped()){
char c = StdDraw.nextKeyTyped();
allElements.handleCharTyped(c);
}
else if(StdDraw.mousePressed()){
allElements.handleClick(StdDraw.mouseX(),StdDraw.mouseY());
Utilities.waitMouseUp();
outbox.displayText("Click "+(++count));
}
else
Utilities.delay();
}
}

}