Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 733513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:15:58+00:00 2026-05-14T07:15:58+00:00

I had two separate interfaces, one ‘MultiLingual’ for choosing language of text to return,

  • 0

I had two separate interfaces, one ‘MultiLingual’ for choosing language of text to return, and second ‘Justification’ to justify returned text. Now I need to join them, but I’m stuck at ‘java.lang.ClassCastException’ error. Class Book is not important here. Data.length and FormattedInt.width correspond to the width of the line. The code :

public interface MultiLingual {
    static int ENG = 0;
    static int PL = 1;
    String get(int lang);
    int setLength(int length);
}

class Book implements MultiLingual {

    private String title;
    private String publisher;
    private String author;
    private int jezyk;

    public Book(String t, String a, String p, int y){

        this(t, a, p, y, 1);
    }

    public Book(String t, String a, String p, int y, int lang){
        title = t;
        author = a;
        publisher = p;
        jezyk = lang;
    }

    public String get(int lang){

        jezyk = lang;
        return this.toString();
    }

    public int setLength(int i){     
        return 0;
    }

    @Override
    public String toString(){

        String dane;
        if (jezyk == ENG){
            dane = "Author: "+this.author+"\n"+
                    "Title: "+this.title+"\n"+
                    "Publisher: "+this.publisher+"\n";
        }
        else {
            dane = "Autor: "+this.author+"\n"+
                    "Tytul: "+this.title+"\n"+
                    "Wydawca: "+this.publisher+"\n";
        }
        return dane;
    }
}


class Data implements MultiLingual {
    private int day;
    private int month;
    private int year;
    private int jezyk;
    private int length;

    public Data(int d, int m, int y){
        this(d, m, y, 1);
    }

    public Data(int d, int m, int y, int lang){
        day = d;
        month = m;
        year = y;
        jezyk = lang;
    }

    public String get(int lang){
        jezyk = lang;
        return this.toString();
    }

    public int setLength(int i){
        length = i;
        return length;
    }

    @Override
    public String toString(){
        String dane="";
        String miesiac="";
        String dzien="";

        switch(month){
            case 1: miesiac="January";
            case 2: miesiac="February";
            case 3: miesiac="March";
            case 4: miesiac="April";
            case 5: miesiac="May";
            case 6: miesiac="June";
            case 7: miesiac="July";
            case 8: miesiac="August";
            case 9: miesiac="September";
            case 10: miesiac="October";
            case 11: miesiac="November";
            case 12: miesiac="December";
        }
        if(day==1){
            dzien="st";
        }
        if(day==2 || day==22){
            dzien="nd";
        }
        if(day==3 || day==23){
            dzien="rd";
        }
        else{
            dzien="th";
        }

        if(jezyk==ENG){
            dane =this.day+dzien+" of "+miesiac+" "+this.year;
        }
        else{
            dane = this.day+"."+this.month+"."+this.year;
        }
        return dane;
    }

}

interface Justification {
    static int RIGHT=1;
    static int LEFT=2;
    static int CENTER=3;

    String justify(int just);

}

class FormattedInt implements Justification {
    private int liczba;
    private int width;
    private int wyrownanie;

    public FormattedInt(int s, int i){
        liczba = s;
        width = i;
        wyrownanie = 2;
    }

    public String justify(int just){

        wyrownanie = just;
        String wynik="";
        String tekst = Integer.toString(liczba);
        int len = tekst.length();
        int space_left=width - len;
        int space_right = space_left;
        int space_center_left = (width - len)/2;
        int space_center_right = width - len - space_center_left -1;
        String puste="";

        if(wyrownanie == LEFT){

            for(int i=0; i<space_right; i++){
                puste = puste + " ";
            }
            wynik = tekst+puste;
        }
        else if(wyrownanie == RIGHT){
            for(int i=0; i<space_left; i++){
                puste = puste + " ";
            }
            wynik = puste+tekst;
        }
        else if(wyrownanie == CENTER){
            for(int i=0; i<space_center_left; i++){
                puste = puste + " ";
            }
            wynik = puste + tekst;
            puste = " ";
            for(int i=0; i< space_center_right; i++){
                puste = puste + " ";
            }
            wynik = wynik + puste;

        }
        return wynik;
    }
}

And the test code that shows this casting “(Justification)gatecrasher[1]” which gives me errors :

  MultiLingual gatecrasher[]={ new Data(3,12,1998),
                               new Data(10,6,1924,MultiLingual.ENG),
                               new Book("Sekret","Rhonda Byrne", "Nowa proza",2007),
                               new Book("Tuesdays with Morrie",
                                        "Mitch Albom", "Time Warner Books",2003,
                                        MultiLingual.ENG),
                             };

  gatecrasher[0].setLength(25);
  gatecrasher[1].setLength(25);

  Justification[] t={ new FormattedInt(345,25),
                      (Justification)gatecrasher[1],
                      (Justification)gatecrasher[0],
                      new FormattedInt(-7,25)
                    };

  System.out.println("         10        20        30");
  System.out.println("123456789 123456789 123456789");
  for(int i=0;i < t.length;i++)
    System.out.println(t[i].justify(Justification.RIGHT)+"<----\n");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:15:58+00:00Added an answer on May 14, 2026 at 7:15 am

    Neither Data or book implement Justification and Multilingual does not inherit Justiifcation so it’s expected that casts like (Justification)gatecrasher[1] will fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That is, if I had two or more sets, and I wanted to return
In my system I have two different projects, one defining its interfaces and another
So I just started my first rails project yesterday. I had two many-to-many (has_and_belongs_to_many)
Has anyone had any success running two different web servers -- such as Apache
I had a problem with committing changes after merging two branches of my project
Has anyone had experience using these two technologies in tandem? What are (if any)
Has anyone had any luck running two instances of the iPhone simulator to test
Ok, I had a simple layout problem a week or two ago. Namely sections
I have two distinct (no inheritance) interfaces: IInvestable and IPricable and two classes: IndexClass
I'm setting up a directory application for which I need to have two separate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.