-3

I was set the ask to print a string of input letters Б О Б using only " * " chars. My Java teacher wants to see the following output:

https://i.stack.imgur.com/zPCJV.png

It's easy to create a public class Letter and to code only two letters using matrix. But I faced the problem to make the whole alphabet (it's an additional task).

Maybe there is another way to turn an input string to picture of " * "?

camickr
  • 321,443
  • 19
  • 166
  • 288
Mike
  • 31
  • 1
  • 7
  • 2
    Welcome to Stack Overflow! Your question is [too broad](https://stackoverflow.com/help/on-topic).This is not a homework completion service. Your instructor gave you the assignment, and you need first to do some effort by yourself. If you can't even get started, ask your teacher for help. Isolate your questions into specific programming snippets, and then search the for answers. In case you need a course or tutorial, Stack Overflow is not the right place to ask it. We wish you good luck in your study. See: [Help Center](https://stackoverflow.com/help). – sɐunıɔןɐqɐp Sep 29 '18 at 14:36
  • Welcome to Stack Overflow! Please take the [tour], look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Assignments aren't usually arbitrary; your instructor, tutorial, or course will have covered the necessary topics to make it possible for you to do this. **Review your course materials, class notes, etc.,** and give it a try. *If* you run into a *specific* problem, research it thoroughly, [search thoroughly here](/help/searching), and if you're still stuck post your code and a description of the problem. People will be glad to help. – T.J. Crowder Sep 29 '18 at 14:38
  • you should make a class for every letter, each class/letter has its own loops, check this https://www.wikihow.com/Use-Java-to-Display-Letters-with-Asterisks – Mina Ragaie Sep 29 '18 at 14:40
  • 1
    Why do you think you have to do anything other than create a `Letter` for all the alphabet? It would be time-consuming and no more informative than doing it for two letters, but that's all there is to it. – Andy Turner Sep 29 '18 at 14:47

2 Answers2

1

I would write something like this:

import java.util.*;
import java.util.stream.*;

public class Alphabet {

    enum Letters {
        A(
                "   *   ",
                "  * *  ",
                " *   * ",
                "*     *",
                "*******",
                "*     *",
                "*     *"
        ),
        B(
                "*****  ",
                "*     *",
                "*     *",
                "*****  ",
                "*     *",
                "*     *",
                "*****  "
        ),
        C(
                " ***** ",
                "*     *",
                "*      ",
                "*      ",
                "*      ",
                "*     *",
                " ***** "
        ),
        None(
                "*******",
                "*******",
                "*******",
                "*******",
                "*******",
                "*******",
                "*******"
        );

        List<String> bitmap;

        Letters(String... bmp) {
            bitmap = Arrays.asList(bmp);
        }

        List<String> strings() {
            return bitmap;
        }
    }

    private final String SPACE = " ";
    private final String origin;
    private final Map<Character, Letters> bitmaps =
            Stream.of(Letters.values())
            .filter(l -> !l.equals(Letters.None))
            .collect(Collectors.toMap(
                    letters -> letters.toString().charAt(0),
                    letters -> letters
            ));

    public Alphabet(String abc) {
        this.origin = abc;
    }

    List<String> data() {
        List<List<String>> word = origin.chars()
                .mapToObj(c -> bitmaps.getOrDefault((char) c, Letters.None).strings())
                .collect(Collectors.toList());

        return IntStream.range(0, word.get(0).size())
                .mapToObj(idx -> word.stream().map(strings -> strings.get(idx)).collect(Collectors.joining(SPACE)))
                .collect(Collectors.toList());
    }

    void print() {
        List<String> bitmap = data();
        bitmap.forEach(System.out::println);
    }

    public static void main(String[] args) {
        new Alphabet(" ABCBA ").print();
    }

}

Pay your time and try to understand how it works. The output will be:

*******    *    *****    *****  *****      *    *******
*******   * *   *     * *     * *     *   * *   *******
*******  *   *  *     * *       *     *  *   *  *******
******* *     * *****   *       *****   *     * *******
******* ******* *     * *       *     * ******* *******
******* *     * *     * *     * *     * *     * *******
******* *     * *****    *****  *****   *     * *******

But there is an idea. You should invent some alghorithm to create a bitmap for each character.

-1

This is my slove for convert number (0 to 9) to char * like your example:

class JavaApplication57 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Letter> ar = new ArrayList<>();
        System.out.println("Please input string: ");
        String s = sc.next();
        for (char c : s.toCharArray()) {
            ar.add(JavaApplication57.convertToLetter(c));
        }
        for (int i = 0; i < 5; i++) {
            for (Letter l : ar) {
                System.out.print(l.getLine(i) + " ");
            }
            System.out.println("");
        }
    }

    static Letter convertToLetter(char ch) {
        String a = "****",
                b = "*  *",
                c = "   *",
                c2 = "*",
                d = "*   ",
                e = "* * ",
                f = "  * ";

        switch (ch) {
            case '0':
                return new Letter(a, b, b, b, a);
            case '1':
                return new Letter(c2, c2, c2, c2, c2);
            case '2':
                return new Letter(a, c, a, d, a);
            case '3':
                return new Letter(a, c, a, c, a);
            case '4':
                return new Letter(d, e, a, f, f);
            case '5':
                return new Letter(a, d, a, c, a);
            case '6':
                return new Letter(a, d, a, b, a);
            case '7':
                return new Letter(a, c, c, c, c);
            case '8':
                return new Letter(a, b, a, b, a);
            case '9':
                return new Letter(a, b, b, c, a);

        }
        return null;
    }

}

class Letter {

    private String line1, line2, line3, line4, line5;

    public Letter(String line1, String line2, String line3, String line4, String line5) {
        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
        this.line4 = line4;
        this.line5 = line5;
    }

    public String getLine(int i) {
        switch (i) {
            case 0:
                return line1;
            case 1:
                return line2;
            case 2:
                return line3;
            case 3:
                return line4;
            case 4:
                return line5;
            default:
                throw new AssertionError();
        }

    }
}

Hope to helpful

  • Thank you for your attention and time you have spent, Lê Đình Bảo Tín, unfortunately I am only a beginner in Java. Could you explain me how does your code work (not in comments, but somwhere else)? – Mike Sep 29 '18 at 15:42
  • Yes, I don' t think you are a beginner. In the easy way, you can think: -class Letter: define a letter, – Lê Đình Bảo Tín Sep 29 '18 at 15:52
  • -Function convertToLetter used to convert char to letter same as your example. -Main: I have an array list to contain all letter, then I print letter via row. – Lê Đình Bảo Tín Sep 29 '18 at 16:01