In: Computer Science
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
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);
}
}