Question

In: Computer Science

JAVA Assignement In the same file, create two classes: a public class Module1 and a non-public...

JAVA Assignement


In the same file, create two classes: a public class Module1 and a non-public (i.e. default visibility) class Module1Data. These classes should have the following data fields and methods:

1) Module1 data fields:
a. an object of type Module1Data named m1d
b. an object of type Scanner named scanner
c. a double named input
2) Module1Data methods:
a. a method named square that returns an int, accepts an int parameter named number, and returns the value of number squared.
b. a method named square that returns a double, accepts a double parameter named number, and returns the value of number squared
c. a method named intCubed that returns an int, accepts a double parameter named number, and typecasts number to an int and then returns the value of number cubed

d(i.e., the value of (int)number cubed, not (int)(number cubed))

d. a method named doubleCubed that returns a double, accepts a double parameter named number, and returns the value of number cubed
3) Module1 methods:
a. a constructor method that creates Module1's Module1Data object (m1d) and Scanner object (scanner, using the System.in argument to read console input)
b. A main method (i.e., with the header public static void main(String[] args)) that, in order:
Creates a new Module1 object by calling Module1's constructor method
Prints the message “Application launched” to the console
Prints a message to the console (use System.out.println()) asking the user to enter afloating point number to be squared and cubed
Reads the user input using the Scanner object's nextDouble() method and assigns the value returned by nextDouble to Module1's input field
Calls m1d's methods (2a – 2d) with input as the argument:
1. 2a: print "int squared: " and the value returned by m1d.square(int) to the console. To call this method, the argument must be typecast to an int
2. 2b: print "double squared: " and the value returned by m1d.square(double) to the console
3. 2c: print "int cubed: " and the value returned by m1d.intCubed() to the console
4. 2d: print "double cubed: " and the value returned by m1d.doubleCubed() to the console
Prints the message “Computations completed” to the console

Additional Constraints

The program should be implemented as a single file holding both classes.
Other than Module1's main method, all data fields & methods should be for individual objects, not the entire class.
Comments should be provided at the start of the file (i.e., above the first class definition) giving the class author’s name and the date the program was finished.

Solutions

Expert Solution

/* No comments the exact comments mentioned in the questions is followed*/

import java.util.*;
import java.lang.*;
import java.io.*;

public class Module1
{
   Module1Data m1d;
   double input;
   Scanner scanner;
   public Module1()
   {
       m1d=new Module1Data();
       scanner=new Scanner(System.in);
   }
   public static void main(String[] args)
   {
       Module1 module1=new Module1();
       System.out.println("Application launched");
       System.out.println("Enter a floating point number to be squared and cubed");
       module1.input=module1.scanner.nextDouble();
       System.out.println("int squared"+module1.m1d.square((int)module1.input));
       System.out.println("double squared"+module1.m1d.square(module1.input));
       System.out.println("int cubed"+module1.m1d.intCubed((int)module1.input));
       System.out.println("double cubed"+module1.m1d.doubleCubed(module1.input));
   }
}

class Module1Data
{
   int square(int number)
   {
       return (int)Math.pow(number,2);
   }
   double square(double number)
   {
       return Math.pow(number,2);
   }
   int intCubed(double number)
   {
       return (int)Math.pow((int)number,3);
   }
   double doubleCubed(double number)
   {
       return Math.pow(number,3);
   }
}

sample output:

Application launched
Enter a floating point number to be squared and cubed
5.5
int squared25
double squared30.25
int cubed125
double cubed166.375


Related Solutions

JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT