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 a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
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.
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c...
WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c = Circle(X, Y, R); 2) Please add a constructor that only accepts R value and initializes the Circle at the origin. 3) Please implement a constructor that behaves the same way as the default constructor. 4) Add a method named “touches” to the Circle class which receives another Circle instance and checks whether these two circles touch each other externally and only at one...
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...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT