Question

In: Computer Science

USE JAVA PLEASE A Phone class has 4 instance variables Model name ("iPhone12","Galaxy") RAM (number of...

USE JAVA PLEASE

A Phone class has 4 instance variables

  • Model name ("iPhone12","Galaxy")

  • RAM (number of gigs such as 8 and12)

  • Storage (number of gigs such as 128 and 512)

  • Screen of type class Screen as described below.

  

A Screen has 3 instance variables:

  • Screen type ("AMOLED", "LCD")

  • Screen size (decimal number such as 6.2 and 10.1)

  • Screen ppi (448, 630, 300)

  

Q1) Code two class-shell for both classes. The names and types of the instance variables have deliberately been left up to you (14m)

  

Q2) Code a 6-parameter contractor for class Phone  (12m)

  • Do not rely on any auto-initialisation

  • You can assume mutators exist for all instance variables

  

Q3) Code the toString method that should print all the specifications of the object that is invoked on. (8m)

Q4) Code a method called normalize() that accepts an array of integers 'intAr' and returns an array of doubles 'doubleAr'. The returned array must have the same length as the input. The method should convert any range of values in 'intAr' into 0..1 range. (10m)

For example:

if intAr=[-100,-50,0,50,100], doubleAr=[0.0,0.25,0.5,0.75,1.0]

How to normalize data?

where:

  • doubleAr[i] is the ith element in the output array doubleAr

  • intAr[i] is the ith element in the input array intAr

  • min(intAr) is the minimum value in the input array intAr

  • max(intAr) is the maximum value in the input array intAr

Solutions

Expert Solution

Answer for Q1, Q2, Q3 are included in a single code:

Summary:

- Created class for phone and screen with given data members and with getters and setters

- Created constructor for phone class with 6 parameters

- Created toString class for both phone and screen class

package mobileapplication;

// class Phone with constructors, getters and setters (Q1)
class Phone{
    private String modelName;
    private int ram;
    private int storage;
    private Screen screen;

    // Constructor for phone class (Q2)
    public Phone(String modelName, int ram, int storage, String screenType, double screensize, int screenppi) {
        this.modelName = modelName;
        this.ram = ram;
        this.storage = storage;
        Screen sc = new Screen(screenType, screensize, screenppi);
        this.screen = sc;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public int getRam() {
        return ram;
    }

    public void setRam(int ram) {
        this.ram = ram;
    }

    public int getStorage() {
        return storage;
    }

    public void setStorage(int storage) {
        this.storage = storage;
    }

    public Screen getScreen() {
        return screen;
    }

    public void setScreen(Screen screen) {
        this.screen = screen;
    }
    
    // toString() class to display
    @Override
    public String toString() {
        return "Phone{ " + " modelName= " + modelName + ", ram=" + ram + ", storage= " + storage + screen.toString() + '}';
    }
    
}

// class Screen with constructors, getters and setters (Q1)
class Screen{
    private String screenType;
    private double screenSize;
    private int screenppi;

    public Screen(String screenType, double screenSize, int screenppi) {
        this.screenType = screenType;
        this.screenSize = screenSize;
        this.screenppi = screenppi;
    }

    public String getScreenType() {
        return screenType;
    }

    public void setScreenType(String screenType) {
        this.screenType = screenType;
    }

    public double getScreenSize() {
        return screenSize;
    }

    public void setScreenSize(double screenSize) {
        this.screenSize = screenSize;
    }

    public int getScreenppi() {
        return screenppi;
    }

    public void setScreenppi(int screenppi) {
        this.screenppi = screenppi;
    }

    @Override
    public String toString() {
        return " Screen{ " + " screenType= " + screenType + ", screenSize=" + screenSize + ", screenppi=" + screenppi + '}';
    }
    
    
}

public class MobileApplication {
    public static void main(String[] args) {
        Phone p1 = new Phone("iPhone12", 8, 128, "AMOLED", 6.2, 448);
        System.out.println(p1.toString());
    }
}

The code for Q4 is as follows. I have provided the necessary comments. Hope this helps.

package testmain;

import java.util.*;


class Compute{
    private int[] array;
    private int size;

    public Compute(int[] array, int size) {
        this.array = array;
        this.size = size;
    }
    
    // Method for normalizing that returns the normalized double array
    public double[] normalize(){
        double[] output = new double[this.size];
        int min = findMin();
        int max = findMax();
        for(int i=0; i<this.size; i++){
            output[i] = (double)(this.array[i]-min)/(double)(max-min);
        }
        return output;
    }
    
    // Method to find min element in min array
    public int findMin(){
        int min = Integer.MAX_VALUE;
        for(int i=0; i<this.size; i++){
            if(this.array[i] < min){
                min = this.array[i];
            }
        }
        return min;
    }
    
    // Method to find max element in max array
    public int findMax(){
        int max = Integer.MIN_VALUE;
        for(int i=0; i<this.size; i++){
            if(this.array[i] > max){
                max = this.array[i];
            }
        }
        return max;
    }
    
    //Method to print the output array
    public void printArray(double arr[]){
        for(int i=0; i<this.size; i++){
            System.out.print(arr[i]+" ");
        }
    }
}

public class TestMain {
    public static void main(String[] args) {
        
        // Get the size and array from the user
        System.out.println("Enter the size: ");
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        int[] array = new int[size];
        System.out.println("Enter all the numbers");
        for(int i=0; i<size; i++){
            int a = sc.nextInt();
            array[i] = a;
        }
        
        // Create a new object of the class Compute
        Compute c = new Compute(array, size);
        
        // Call the method normalize
        double[] op = c.normalize();
        
        // Call the method printArray to print the output
        c.printArray(op);
    }
    
}

Related Solutions

Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that...
SalaryCalculator in Java. The SalaryCalculator class should have instance variables of: an employee's name, reportID that is unique and increments by 10, and an hourly wage. There should also be some constructors and mutators, and accessor methods.
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
In Java The Order class should have:  Seven instance variables: the order number (an int),...
In Java The Order class should have:  Seven instance variables: the order number (an int), the Customer who made the order, the Restaurant that receives the order, the FoodApp through which the order was placed, a list of food items ordered (stored as an array of FoodItem), the total number of items ordered (an int), and the total price of the order (a double).  A class constant to set the maximum number of items that can be ordered...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class Create another class called CourseSection Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of the methods required for a standard user defined...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name...
IN JAVA ECLIPSE PLEASE The xxx_Student class A Student has a: – Name - the name consists of the First and Last name separated by a space. – Student Id – a whole number automatically assigned in the student class – Student id numbers start at 100. The numbers are assigned using a static variable in the Student class • Include all instance variables • Getters and setters for instance variables • A static variable used to assign the student...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT