Casting interfaces in java
Posted
by owca
on Stack Overflow
See other posts from Stack Overflow
or by owca
Published on 2010-04-12T02:25:22Z
Indexed on
2010/04/12
2:33 UTC
Read the original article
Hit count: 595
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");
© Stack Overflow or respective owner