Question

In: Computer Science

Create an application that uses a constructor and two different methods. JAVA

Create an application that uses a constructor and two different methods.

JAVA

Solutions

Expert Solution

Java Console Application that calculates area of a square:

import java.util.Scanner;

//Class definition
class Square
{
   //Private instance variable
   private double side;
  
   //Default Constructor
   public Square()
   {
       //Set side value
       setSide(1.0);
   }
  
   //Argument Constructor
   public Square(double tSide)
   {
       //Set side value
       setSide(tSide);
   }
  
   //Set method for side
   public void setSide(double tSide)
   {
       //Set side value
       side = tSide;
   }
  
   //Get method for side
   public double getSide()
   {
       //Get side value
       return side;
   }
  
   //Computing area
   public double computeArea()
   {
       //Computing area
       return side*side;
   }
  
   //Returning string version
   public String toString()
   {
       return "Square Side: " + side + " \t Area of the Square: " + computeArea();
   }
}

//Driver class
public class SquareTest
{
   // set up GUI components and instantiate new MyRectangle
   public static void main( String args[] )
   {
       Scanner input = new Scanner( System.in );
      
       // create a new Square with no initial values
       Square square = new Square();

       System.out.print( "Please enter a double value for the side of the square: " );
       double double1 = input.nextDouble();

       square.setSide( double1 );

       System.out.println( square ); // see the results of the test

   } // end main
  
} // end class SquareTest

____________________________________________________________________________________________

Sample Run:


Related Solutions

Create an Java application that uses a class to convert number grades to letter grades and...
Create an Java application that uses a class to convert number grades to letter grades and another class for data validation. Specifications The Grade class should have only one Instance Variable of type int. Use a class named Grade to store the data for each grade. This class should include these three methods:   public void setNumber(int number)   public int getNumber()   public String getLetter() The Grade class should have two constructors. The first one should accept no parameters and set the...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. Student has a String name, a double GPA, and a reasonable equals() method. Course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses binary file I/O to save and retrieve Students and Lists of Students. CoursePersister does...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
Create a Java application that meets the following specifications. Write whatever public and private methods are...
Create a Java application that meets the following specifications. Write whatever public and private methods are necessary. Put all of these classes in a package called persistence1. The student has a String name, a double GPA, and a reasonable equals() method. The course has a String name, an ArrayList of Students, and a reasonable equals() method. It will also need a method to add a Student. StudentPersister uses the binary file I/O to save and retrieve Students and Lists of...
Create a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters.
JAVACreate a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0.
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
JAVA PROJECT Step 1: Create an application named YourInitials_Project 3 that uses a class to convert...
JAVA PROJECT Step 1: Create an application named YourInitials_Project 3 that uses a class to convert number grades to letter grades and another class for data validation. Make comments including your name, date, and project name as well as appropriate comments throughout your application that include the step number. Step 2: Create code to print Titles Step 3: Create a variable to hold user’s choice. Remember that all variables must begin with your initials and should be descriptive. Step 4:...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for the given class. Create an instance of the class in a main method using a Practice class. Note: The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void.      
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT