Question

Complete the code MyImage and test in your own main Public class MyImage { private char[][]...

Complete the code MyImage and test in your own main
Public class MyImage {
    private char[][] pixels;
    private int imageSize;

    //constructor
    public MyImage(int imageSize) {
        pixels = new char[imageSize][imageSize];
        for (int i = 0; i < imageSize; ++i) {
            for (int j = 0; j < imageSize; ++j) {
                pixels[i][j] = '.';
            }
        }
    }

    //print image
    public void print(){
        //add your code

    }

    //draw a triangle with '*'
    public void drawTriangle() {
        //add your code

    }

    //draw a diamond with '*'
    public void drawDiamond() {
        //add your code

    }

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

Please find the code below

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

public class MyImage {

      private char[][] pixels;

      private int imageSize;

      /**

      * Constructor

      * @param imageSize

      */

      public MyImage(int imageSize) {

            pixels = new char[imageSize][imageSize];

            for (int i = 0; i < imageSize; ++i) {

                  for (int j = 0; j < imageSize; ++j) {

                        pixels[i][j] = '.';

                  }

            }

      }

      /**

      * Print image

      * @param print

      */

      public void print(String print){

            System.out.print(print);

}

      /**

      * Draw a triangle with '*'

    * @param imageSize

      */

      public void drawTriangle(int imageSize) {

            for(int i = 1; i <= imageSize; i ++) {

                  for(int j = 0; j < imageSize - i; j ++) {

                        print(" ");

                  }

                  for(int j = 0; j < (2 * i - 1); j ++) {

                        print("*");

                  }

                  print("\n");

            }

      }

      /**

      * Draw a diamond with '*'

      * @param imageSize

      */

      public void drawDiamond(int imageSize) {

            for(int i = 0; i <= imageSize; i ++) {

                  for(int j = 1; j <= imageSize - i; j++) {

                        print(" ");

                  }

                  for(int k = 1; k <= (2 * i - 1); k ++) {

                        print("*");

                  }

                  print("\n");

            }

            for(int i = imageSize - 1; i >= 1; i --) {

                  for(int j = 1; j <= imageSize - i; j ++) {

                        print(" ");

                  }

                  for(int k = 1; k <= (2 * i - 1); k ++) {

                        print("*");

                  }

                  print("\n");

            }

      }

      public static void main(String[] args) {

            MyImage image = new MyImage(4);

            image.drawTriangle(8);

            image.drawDiamond(7);

      }

}

Add a comment
Know the answer?
Add Answer to:
Complete the code MyImage and test in your own main Public class MyImage { private char[][]...
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
  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean...

  • The following class class studentType { private: char firstName[25]; char lastName[25]; int testScore[4]; double averageScore; public:...

    The following class class studentType { private: char firstName[25]; char lastName[25]; int testScore[4]; double averageScore; public: }; int main(){ studentType student[30]; } Write c++ code ti create a constructor of studentType to initialize the member variables. Also write function to input values of the member variables and print the values

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info;...

    public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info; this.link = null; } public CharNode(char c, CharNode link) { this.info = info; this.link = link; } public CharNode getLink() { return link; } public void setLink(CharNode link) { this.link = link; } public char getInfo() { return info; } public void setInfo(char info) { this.info = info; } } //Remember Queue is a first-in-first-out data structure public class CharQueue { // front is...

  • Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class...

    Complete StackArray.java code below. Use StackArrayDemo.java to test your implementation of StackArray.java. StackArray.java below. public class StackArray <T> { public static int CAPACITY = 100; private final T[] elements; private int topIndex; // Constructor public StackArray() { // Initialize elements // Initialize topIndex to -1 } public T peek() { // If topIndex is less than zero, return null. // Otherwise, return element from top of the stack. } public T pop() { // If topIndex is less than zero,...

  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...

  • public class Test { private static int i =0; private static int j =0; public static...

    public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...

  • Analyze the following code: public class Test { private int t; public static void main(String[] args)...

    Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } The variable t is private and therefore cannot be accessed in the main method. The program compiles and runs fine. t is non-static and it cannot be referenced in a static context in the main method. The variablet is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.

  • class Test public static void main(String args) { Aa=new AO: a.printo: class A private String s;...

    class Test public static void main(String args) { Aa=new AO: a.printo: class A private String s; public A (String news) { 8 = news: public void print { System.out.println(s): The program would compile and run if you change A a new Alto Aa=new A('5'). The program has a compilation error because the instance variables in class A is not public. The program has a compilation error because class A does not have a no-arguments constructor The program compiles and runs...

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