Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 14, Problem 4FTE
Explanation of Solution
Given code:
public class MyPanel extends JPanel //Line 1
{
public MyPanel() //Line 2
{
//Constructor code...
}
public void paint(Graphics g) //Line 3
{
//paint method code...
}
}
Drawing on panels:
- User can draw on a panel by simply obtaining a reference to the panels “Graphics” object and then drawing using that object’s method.
- So, the resulting graphics are drawn only on the panel.
- The “paintComponent” method helps for “JPanel”.
- This method is automatically called once the component requires to be redisplayed.
- When this method is called, this method takes the argument of “Graphics” object.
- The header for this method’s is shown below:
public void paintComponent(Graphics g)
- When user override the above method, first the user should call the superclass “paintComponent” method to confirm that the given component is correctly showed...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
public class DebugBox
{
privateint width;
privateint length;
privateint height;
publicDebugBox()
{
length = 1;
width = 1;
height = 1;
}
publicDebugBox(intwidth,intlength,intheight)
{
width = width;
length = length;
height = height;
}
publicvoidshowData()
{
System.out.println("Width: " + width + "Length: " +
length + "Height: "+ height);
}
publicdoublegetVolume()
{
double vol = length * width * height;
return vol;
}
}
// This class uses a DebugBox class to instantiate two Box objects
public class DebugFour3
{
publicstaticvoidmain(Stringargs[])
{
int width =12,
length = 10,
height = 8;
DebugBox box1 =newDebugBox();
DebugBox box2 =newDebugBox(width, length, height);
System.out.println("The dimensions of the first box are");
box1.showData();
System.out.print("The volume of the first box is ");
showVolume(box1);
System.out.println("The dimensions of the second box are");
box2.showData();
System.out.print("The volume of the second box is ");
showVolume(box2);
}…
Please check the images for the whole question. Thank you.
/* TestCarSensor.java - program to test the CarSensor class.*/public class TestCarSensor{public static void main (String[] args){// 1. declare CarSensor objectsCarSensor generic = new CarSensor(); // default sensor, all values "zero"CarSensor tempCel = new CarSensor("temperature sensor", -50, +300, "C"); // temperature sensor (Celsius)CarSensor speed = new CarSensor("speed sensor", 0, 200, "km/h"); // speed sensor (kms/hour)CarSensor speed2 = new CarSensor("speed sensor 2", 0, 200, "m/h"); // speed sensor 2 (miles/hour)// 2. test changing desc and limitsSystem.out.println ();System.out.println ( generic ); // display generic sensor (zero)generic.setDesc ("special sensor"); // change descriptiongeneric.setLimits (-5,5,"units"); // change limitsSystem.out.println ( generic ); // display generic sensor again// 3. test displaying object (calling .toString() )System.out.println ();System.out.println ( tempCel );System.out.println (…
toString() and equals() Methods
This lab will demonstrate how the toString() and equals() methods work when they are
not overridden. Type up the code and submit the BOTH results.
CODE
public class ObjectToString(
public static void main(String[] args) {
ClassOne co new ClassOne();
ClassTwo ct = new ClassTwo();
}
}
public class Classone {
System.out.println(co);
System.out.println(ct);
System.out.println(co.equals(ct));
public void printfle() {
System.out.println("I am from ClassOne");
}
public class ClassTwo (
public void printMe() {
}
System.out.println("I am from ClassTwo");
Run the program and notice the output.
Chapter 14 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Ch. 14.1 - Prob. 14.1CPCh. 14.1 - Prob. 14.2CPCh. 14.2 - Prob. 14.3CPCh. 14.2 - Prob. 14.4CPCh. 14.2 - Prob. 14.5CPCh. 14.2 - Prob. 14.6CPCh. 14.2 - Prob. 14.7CPCh. 14.2 - Prob. 14.8CPCh. 14.2 - Prob. 14.9CPCh. 14.2 - Prob. 14.10CP
Ch. 14.2 - Prob. 14.11CPCh. 14.3 - Prob. 14.12CPCh. 14.3 - Prob. 14.13CPCh. 14.3 - Prob. 14.14CPCh. 14.3 - Prob. 14.15CPCh. 14.4 - Prob. 14.16CPCh. 14.4 - Prob. 14.17CPCh. 14.5 - Prob. 14.18CPCh. 14.5 - Prob. 14.19CPCh. 14.5 - Prob. 14.20CPCh. 14.5 - Prob. 14.21CPCh. 14.5 - Prob. 14.22CPCh. 14.5 - Prob. 14.23CPCh. 14.5 - Prob. 14.24CPCh. 14.5 - What Graphics class methods do you use to perform...Ch. 14.6 - Prob. 14.26CPCh. 14.6 - Prob. 14.27CPCh. 14.6 - Prob. 14.28CPCh. 14.6 - Prob. 14.29CPCh. 14.6 - Prob. 14.30CPCh. 14.7 - Prob. 14.31CPCh. 14.7 - Prob. 14.32CPCh. 14.7 - Prob. 14.33CPCh. 14.7 - Prob. 14.34CPCh. 14.8 - Prob. 14.35CPCh. 14.8 - Prob. 14.36CPCh. 14.8 - Prob. 14.37CPCh. 14.8 - Prob. 14.38CPCh. 14 - Prob. 1MCCh. 14 - Prob. 2MCCh. 14 - Prob. 3MCCh. 14 - Prob. 4MCCh. 14 - When using Swing to write an applet, you extend...Ch. 14 - Prob. 6MCCh. 14 - Prob. 7MCCh. 14 - Prob. 8MCCh. 14 - Prob. 9MCCh. 14 - Prob. 10MCCh. 14 - Prob. 11MCCh. 14 - Prob. 12MCCh. 14 - Prob. 13MCCh. 14 - Prob. 14MCCh. 14 - Prob. 15MCCh. 14 - Prob. 16MCCh. 14 - Prob. 17MCCh. 14 - Prob. 18MCCh. 14 - Prob. 19MCCh. 14 - Prob. 20MCCh. 14 - Prob. 21MCCh. 14 - Prob. 22MCCh. 14 - Prob. 23TFCh. 14 - Prob. 24TFCh. 14 - Prob. 25TFCh. 14 - Prob. 26TFCh. 14 - Prob. 27TFCh. 14 - Prob. 28TFCh. 14 - Prob. 29TFCh. 14 - Prob. 30TFCh. 14 - Prob. 31TFCh. 14 - Prob. 32TFCh. 14 - Prob. 33TFCh. 14 - Prob. 34TFCh. 14 - Prob. 35TFCh. 14 - Prob. 1FTECh. 14 - Prob. 2FTECh. 14 - Prob. 3FTECh. 14 - Prob. 4FTECh. 14 - Prob. 5FTECh. 14 - Prob. 6FTECh. 14 - Prob. 1AWCh. 14 - Prob. 2AWCh. 14 - Prob. 3AWCh. 14 - Prob. 4AWCh. 14 - Prob. 5AWCh. 14 - Prob. 6AWCh. 14 - Prob. 1SACh. 14 - Prob. 2SACh. 14 - Prob. 3SACh. 14 - Prob. 4SACh. 14 - Prob. 5SACh. 14 - Prob. 6SACh. 14 - Prob. 7SACh. 14 - Prob. 8SACh. 14 - Prob. 9SACh. 14 - Prob. 1PCCh. 14 - House Applet Write an applet that draws the house...Ch. 14 - Prob. 3PCCh. 14 - Thermometer Applet Write an applet that displays a...Ch. 14 - Prob. 5PCCh. 14 - Prob. 6PCCh. 14 - Prob. 7PCCh. 14 - Prob. 8PCCh. 14 - Prob. 9PC
Knowledge Booster
Similar questions
- class Param3 { public int x; private void increase(int p) { x = x*p; } public void calculateX(int y) { increase(y); } public int getX() { return x; } } // in another class Param3 q3 = new Param3(); q3.x = 5; q3.calculateX(7); System.out.println(q3.getX()); what would be the answer for the last two lines ? also above were x = x*p do both x in here are the fields? wouldn't that be cnofusing?arrow_forwardComplete the missing line of code: public class Rectangle { private int width; private int height; public Rectangle( this.width = width3B %3D this.height = height; %3Darrow_forwardpackage debugging; public class BeanCounter {public static int beanCount = 0;} public class Bean {public Color beanColor;public String description;public Bean(Color beanColor, String description){this.beanColor = beanColor;this.description = description;}} public class Color {double[] rgb;double saturation;public Color(double[] rgb, double saturation){this.rgb = rgb;this.saturation = 0.0;}} public class Debugging { /*** @param args the command line arguments*/public static void main(String[] args) {BeanCounter bc = new BeanCounter();double[] redGreenBlue = {22.3d, 41.7d, 202.57d};Color c = new Color(rgb, 22.44+0.05, 15.78, 79.53, 252.04);++BeanCounter.beanCount;Bean b = new FavaBean(c); // New bean created!bc.beanCount++;}} 1. Remove the errors, warnings and mistakes that are in the source code. 2. Modify the code so that: •one bean is created •one bean is counted •there is only one line in the main methodarrow_forward
- public class MyGenClass { private T1 name; private T2 stulD; private T3 CGPA; public void setStuData( ){ I/ parameters name = n; stulD = d: CGPA = g; } public void printAsString() { System.out.println( } ); I/ print all information as string public static void main(String args||) { Il Create an object stu by parameters String, Integer, Double Il Call setStuData() with any values you like /l Call printAsString() } }arrow_forwardpublic class Geometry{public static void main(String[] args){//asks for user's choice Scanner in = new Scanner(System.in);displayMenu();selectOption(choice);System.out.println("Enter your choice (1-3): ");int choice1 = in.nextInt();System.out.println("Thanks for using the Geometry Calculator - Goodbye!");//Prints if input is a number that is not one of the choiceswhile(choice1 < 1 || choice1 > 3 ){System.out.println("Invalid choice. Please enter 1 - 3: ");choice1 = in.nextInt();}}/**Displays the menu*/public static void displayMenu(){System.out.println("Welcome to the Geometry Calculator Application");System.out.println("1. Calculate the Area of a Circle");System.out.println("2. Calculate the Area of a Rectangle");System.out.println("3. Calculate the Area of a Triangle");}/** This method calculates the area of the circle@return the area of the circle*/public static double circle()// calculates the area of the circle{Scanner in = new Scanner(System.in);System.out.println("What is…arrow_forwardCreate a subclass named Square from the superclass Geometricobject. Create an instance variable named width with initial value 0. Create a no-arg constructor. Create the another constructor, which has one parameter named wid. ASsign the wid to the instance variable named width. Create the getWidth() method, the setWidth() method. Override the getArea() method. Override the getPerimeter() method. Create a test class named Test to create an instance with wid= 6 from Square class. Call the getArea() and getPermeter() methods to print out the area and perimeter of the square with width=6.arrow_forward
- // define the interface interface Measurable { public abstract double getArea(); } // create an abstract class shape abstract class Shape implements Measurable { String type; String name; // constructor of Shape class Shape(String name) { this.name = name; } // accessor for type public String getType() { return type; } // accessor for name public String name() { return name; } // setter for name public void setName(String name) { this.name = name; } // define toString method public String toString() { return (type + " : " + name); } } // define Rectangle class that extends Shape class class Rectangle extends Shape { double width; double height; Rectangle(String name, double width, double height) { super(name); this.type = "rectangle"; this.width = width; this.height = height; } public double getWidth() {…arrow_forwardAnalyze the code below and answer the questions that follows.class Shapes {public void identify () {System.out.println("Identifying Shapes...");}}class Square extends Shapes {public void identify () {System.out.println("Identifying Square...");}}class Circle extends Shapes {170public void identify () {System.out.println("Identifying Circle...");}}class Main {public static void main(String[] args) {Square s1 = new Square();s1.identify();Circle c1 = new Circle();c1.identify();}}a. What OOP concept (s) is illustrated in the code? Explain your answer.b. What is the output of the code?arrow_forwardAdvanced Object Concepts. I need help in understanding this solution, how everything works and relates with each other.arrow_forward
- public class Polygon { private double sideLength; private int sides; private String shape; public double getArea () { int n sides; double a sideLength; return ((a * a * n) / (4 * Math.tan((180 / n) 3.14159 / 180))); public void setPolygon (double sl, int sd, String sh) { sides-sd; sideLength-sl; shape=sh; public Polygon (double sl, int sd, String sh) ( sides-sd; sideLengthsl; shape=sh; public String getShape () { return (shape); } public int getSides () { return (sides); } public double getLength () { return (sideLength); } Write a static method named find that takes as a parameter an array of type Polygon named list. The method should print the shapes of each polygon that has an area greater than 500 and return their count. The method prototype is public static int find (Polygon [] list); Note: assume the array is already declared, populated with data in the main method and ready to be used.arrow_forwardPlease do it in JavaScript or C# 1) Design and implement the class structure for a drawing application. A drawing is made up of a canvas that contains a collection of three different shapes: circles, lines, and boxes. Each shape can be drawn by calling a Draw() method for that shape. The canvas class should also have a draw method that draws all of the shapes it contains. You don’t have to actually draw anything on a screen; you can simulate drawing by writing a message to the console.For example, the code snippet: canvas.Draw();Drawing a circle at...Drawing a line at...etc. (For each shape on the canvas) 2)(Bonus) Consider re-writing the above answer (#1) differently where the canvas does not contain the shapes, but is able to draw any of the three shapes passed to it. Re-design canvas.Draw() if you need to. Your main() program should ask for user input on what shape to draw. Ask once, draw, then exit.could produce something like the following output to the console:arrow_forwardplease modify this: display method must call displayNormal Window class abstract class Window implements GUIComponent {private int height, width;private boolean isThisVisible, minimized; public Window(int width, int height){this.height = height;this.width = width;} public int getHeight() {return height; } public int getWidth() {return width; } public String toString() { return "a " + width + "x" + height + " minimal window"; }public void resize(int width, int height){this.width = width;this.height = height;}public void display() {if (!isThisVisible) {System.out.println("(Nothing to see here)");}else if (!minimized) { System.out.println(".......................\n:" + toString() + ":\n.......................");} else {System.out.println("[" + this.toString() + " (minimized)]");} }public void minimize(){ minimized = true;} public void setVisible(boolean isThisVisible) { this.isThisVisible = isThisVisible; }public void restore(){ minimized=false;} public boolean isVisible() { return…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,