Given the existence of a Phone class. Define a subclass, CameraPhone, that conta
ID: 3567217 • Letter: G
Question
Given the existence of a Phone class.
Define a subclass, CameraPhone, that contains two instance variables: an integer named, imageSize, representing the size in megapixels (for simplicity assume a pixel takes up one byte -- thus megapixels equals megabytes) of each picture (i.e., 2 means each image is composed of 2 megapixels), and an integer named memorySize, representing the number of gigabytes in the camera's memory (i.e., 4 means 4 Gigabyes of memory).
There is a constructor that accepts two integer parameters corresponding to the above two instance variables and which are used to initialize the respective instance variables.
There is also a method named numPictures that returns (as an integer) the number of pictures the camera's memory can hold.
Don't forget that a gigabyte is 1,000 megabytes.
------------------------------------------------------------------------
import java.util.Scanner;
class Phone{
private int ph_ID;
}
///{
//write your code here
//start
//end
///}
public class inheritance{
public static void main(String[] arg){
Scanner scanner =new Scanner(System.in);
System.out.println("Enter the imagesize:");
int imagesize=scanner.nextInt();
System.out.println("Enter the memorysize:");
int memorysize=scanner.nextInt();
CameraPhone cph=new CameraPhone(imagesize,memorysize);
System.out.println("imagesize:"+cph.getImageSize());
System.out.println("memorysize:"+cph.getMemorySize());
System.out.println("numPictures:"+cph.numPictures());
}
}
Explanation / Answer
public class CameraPhone extends Phone{
int imageSize;
int memorySize;
CameraPhone(int is, int ms){
imageSize = is;
memorySize = ms;
}
int getImageSize(){
return imageSize;
}
int getMemorySize(){
return memorySize;
}
int numPictures(){
int numPictures = memorySize * 1000 / imageSize;
return numPictures;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.