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 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...
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...
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...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
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...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
(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(),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT