Question

In a new file located in the same package as the class Main, create a public...

In a new file located in the same package as the class Main, create a public Java class to represent a photograph that consists of a linear (not 2D) array of pixels. Each pixel is stored as an integer. The photograph class must have:

(a) Two private fields to represent the information stored about the photograph. These are the array of integers and the date the photograph was taken (stored as a String). The values in the array must be in the range 0 ≤ pixel ≤ 255. The default size of the array is 100, and the default date is “19000101”. A valid array size is at least 4, and a valid date has exactly 8 characters (e.g., 19000101 for January 1, 1900).

(b) A public “getter” for the date field, a public method to return the size of the pixel array, and a public method setPixel to change an individual element in the pixel array. The setPixel method receives two parameters: the index of the pixel to be changed and the value it is to be set to as long as both are valid based on the size of the array and the range given above.

(c) A public “default” constructor, (i.e., a constructor that receives no parameters). The default constructor may initialize the class fields or default initial values may be assigned to the class fields at their declaration.

(d) A public parametrized constructor that receives two parameters, the size of the pixel array and the date the photograph was taken. The two fields are initialized with the received values if the received values are valid.

(e) The three static “factory” methods given at the end of this document. Note that the class name is chosen as “Photograph” and may need to be changed given the name of your class.

4. In class Main, use the photograph class to create reference variables and objects using each of the three factor methods. Display the size and date of each object by calling the appropriate “getter” on the appropriate reference variable. The sizes and dates for these test objects have been hard-coded.

5. In a new file located in the same package as the class Main, create a public Java class to represent a camera that consists of a memory card able to store a number of photographs (represented by an array of reference variables), a zoom lens (represented by a zoom level), a calendar holding the current date (represented as a String), and a button to take a picture (represented by two methods to add a photograph reference to the memory card). The camera class must have the following:

(a) Five private fields as data members:

i. An array of photograph reference variables. The size of the array (i.e., memory card) is set when the camera class object is created, but the array’s size must be at least 16 regardless of user input.

ii. The number of photographs currently in the memory card (i.e., the number of photograph objects currently being referenced by elements of the array). The number of photographs referenced in the array always begins at zero.

iii. The current zoom level stored as a floating point number. The zoom level defaults to 1.0 and is always in the range 1.0 ≤ zoom ≤ 4.0.

iv. The current date stored as a String. The date defaults to January 1, 1900 and always has exactly 8 characters (see above).

(b) A parameterized constructor that receives two parameters giving the maximum number of photographs (i.e., giving the size of the array of photograph class type to be created) and the current date. The photograph array reference variable is assigned to a new photograph array object of the size value received, but with at least 16 elements.

(c) A default constructor that sets the fields to their default or minimum values and otherwise acts just as the parameterized constructor.

(d) A public “getter” (but no “setter”) method for the number of photographs field. This returns the number of photographs stored in the array, not the number of elements in the array.

(e) Public “getter” and “setter” methods for each of the zoom level and date fields. The “setter” methods must enforce the rules for valid values given above.

(f) Two public methods that each add one photograph to the memory card as long as there is room in the array of photographs. Note that the number of photographs field is the location of the next available element in the array.

i. One boolean method to add a photograph that receives one parameter, a photograph reference variable, and stores the reference in the first available position in the photograph array. Upon successfully storing the photograph, the number of photographs is increased and true is returned. Otherwise, false is returned.

ii. Another boolean method to add a photograph that receives one integer parameter, the number of pixels in the photograph. The method then creates a new photograph class object with the given number of pixels and the date from the camera’s calendar. Each pixel of the photograph is set to a random integer in the range of valid values given above. The reference to the new object is stored in the first available position in the photograph array. Upon successfully storing the photograph, the number of photographs is increased and true is returned. Otherwise, false is returned.

(g) A public int method that receives a photograph number that is used as the index in the photograph array and returns the size of the photograph. If the photograph number received is outside of the range of indexes or refers to an element in the array that does not reference a photograph, the value -1 is returned.

(h) A public String method that receives a photograph number that is used as the index in the photograph array and returns the date of the photograph. If the photograph number received is outside of the range of indexes or refers to an element in the array that does not reference a photograph, the value “00000000” is returned.

6. In class Main, use the camera and photograph classes to create an application that allows the user to create then manage a virtual camera. Use user input to set the number of photographs allowed in the camera object and the current date, then, in a loop, present a menu of options for the user to select. The loop should continue until the user elects to stop. The minimum options in the menu are:

(a) Display the number of photographs in the camera

(b) Add a photograph to the camera by getting its number of pixels from the user and indicate whether or not the photograph was added successfully.

(c) Add a photograph to the camera by giving a factory-defined object and indicate whether or not the photograph was added successfully. You may use one of the static “factory” methods to create a photograph object. • createAll255sPhotograph() • createMinimumPhotograph() • create7x7Checkerboard()

(d) Choose one of : Set the calendar date of the camera to a value from the user (or) Display the date and size of all photographs in the camera (e) Choose one of : Display the date and size of the oldest photograph in the camera (e.g., by using String’s compareTo method) (or) Display the date and size of the largest photograph in the camera (e.g., by comparing the sizes of the pixel arrays)

(f) Halt the application

Example Program Executions

User input is indicated as red text.

(Example For Items 3 and 4)

createAll255sPhotograph Factory: 100 19000101

createMinimumPhotograph Factory: 4 19000102

create7x7Checkerboard Factory: 49 19000103

(Example for Items 5 and 6)

Enter the size of the memory card: 16

Enter the current date in the format YYYYMMDD: 20201020

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 1

There are 0 photographs in the camera.

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 2

Enter the photo’s size: 1024

(*) New photo added at position 0 with size 1024

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 3

Which of the following factories would you like:

a) All 255’s

b) Minimum

c) Checkerboard

a

(*) New photo added at position 1 with size 4

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 3

Which of the following factories would you like:

a) All 255’s

b) Minimum

c) Checkerboard

b

(*) New photo added at position 2 with size 4

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 3

Which of the following factories would you like:

a) All 255’s

b) Minimum

c) Checkerboard

c

(*) New photo added at position 3 with size 4

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 5

The oldest photo was taken on 19000101 and it has size 100

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 6

The largest photo size is 1024 and it was taken on 20201020

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: 7

Memory Card Contents:

[0] date: 20201020 size: 1024

[1] date: 19000101 size: 100

[2] date: 19000102 size: 4

[3] date: 19000103 size: 49

1) Display the number of photographs

2) Add a randomly-generated photograph

3) Add a factory-generated photograph

4) Set the camera’s date

5) Display the date and size of the oldest photograph

6) Display the date and size of the largest photograph

7) Display the date and size of photographs in the camera’s memory

Q) Halt the application

Select a menu choice: Q

Additional Requirements

The following coding and implementation details must be present in your solution to receive full credit for Programming Assignment #4.

1. Numbered items 3 and 4 (above) must be turned in as Part A of this Programming Assignment. See Moodle for due dates and additional details.

2. A reference variable and instance object of class java.util.Scanner must be used to read the input from the user.

3. Identifiers must be descriptive (i.e., must self document).

4. Indention of all code blocks (compound statements, code inside braces), including single statements following selection or while statements, is required.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution code in java for first question ( containing first 5 parts):

CODE

================================================================

import java.awt.image.BufferedImage;

import java.awt.image.DataBufferByte;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Photograph {

    public int width;

    public int height;

    private boolean hasAlphaChannel;

    private int pixelLength;

    private int[] pixels;

private size;

private String photoDate;

Photograph()

{

pixels[]=pixels[100];

}

Photograph (int pixelLength, String photoDate)

{

pixels=new pixels[pixellength];

this.photoDate=photoDate;

}

    Photograph (BufferedImage image) {

        pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

        width = image.getWidth();

        height = image.getHeight();

        hasAlphaChannel = image.getAlphaRaster() != null;

        pixelLength = 3;

        if (hasAlphaChannel)

            pixelLength = 4;

    }

public int getPixel(int[] pixels)

{

return pixelLength;

}

public void setPixel(int index, int value)

{

pixels[index]=value;

}

/* To get values of RGB)*/

    int[] getRGB(int x, int y) {

        int pos = (y * pixelLength * width) + (x * pixelLength);

        int rgb[] = new int[4];

        if (hasAlphaChannel)

            rgb[3] = (int) (pixels[pos++] & 0xFF); // Alpha

        rgb[2] = (int) (pixels[pos++] & 0xFF); // Blue

        rgb[1] = (int) (pixels[pos++] & 0xFF); // Green

        rgb[0] = (int) (pixels[pos++] & 0xFF); // Red

        return rgb;

    }

}

===================================================================

Solution code in java for second question(question no:4) to demonstrate the getter and setter methods to get and set pixels of a given image:

==========================================================================

import java.io.File;

import java.io.IOException;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class Main

{

    public static void main(String args[])throws IOException

    {

        BufferedImage img = null;

        File f = null;

        //read image

        try

        {

            f = new File("G:\\Inp.jpg");

            img = ImageIO.read(f);

        }

        catch(IOException e)

        {

            System.out.println(e);

        }

        //get image width and height

        int width = img.getWidth();

        int height = img.getHeight();

  

        /* get pixel value */

        int p = img.getRGB(0,0);

        /* We, have seen that the components of pixel occupy 8 bits. To get the bits we have to first right shift the 32 bits of the pixels by bit position and then bitwise ADD it with 0xFF. 0xFF is the hexadecimal representation of the decimal value 255. */

        // get alpha

        int a = (p>>24) & 0xff;

        // get red

        int r = (p>>16) & 0xff;

        // get green

        int g = (p>>8) & 0xff;

        // get blue

        int a = p & 0xff;

        /*

        for simplicity we will set the ARGB  value to 255, 100, 150 and 200 respectively.

        */

        a = 255;

        r = 100;

        g = 150;

        b = 200;

        //set the pixel value

        p = (a<<24) | (r<<16) | (g<<8) | b;

        img.setRGB(0, 0, p);

        //write image

        try

        {

            f = new File("G:\\Out.jpg");

            ImageIO.write(img, "jpg", f);

        }

        catch(IOException e)

        {

            System.out.println(e);

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
In a new file located in the same package as the class Main, create a public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

  • create java application with the following specifications: -create an employee class with the following attibutes: name...

    create java application with the following specifications: -create an employee class with the following attibutes: name and salary -add constuctor with arguments to initialize the attributes to valid values -write corresponding get and set methods for the attributes -add a method in Employee called verify() that returns true/false and validates that the salary falls in range1,000.00-99,999.99 Add a test application class to allow the user to enter employee information and display output: -using input(either scanner or jOption), allow user to...

  • Programming: Create a class called City with two public variables: a string to store the name...

    Programming: Create a class called City with two public variables: a string to store the name of the city and a float to store the average temperature in Fahrenheit. Create one object of the class that you create and ask the user to enter the city name and the average temperature in Fahrenheit. Store these in the object of the class that you create. Then display the contents of the class. Once that is working, make the variables private and...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • Use your Car class from the previous Programming Assignment. For this assignment, create a new class...

    Use your Car class from the previous Programming Assignment. For this assignment, create a new class that represents a Used Car Dealership. Your Java program will simulate an inventory system for the dealership, where the employees can manage the car information. Start by creating an array of 10 cars, with an "ID" of 0 through 9, and use the default constructor to create each car. For example, the third car in an array called cars would have ID 2. You...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT