Ajuda com efeito colateral

5 respostas
jane_bravo

olá…eu to fazendu um exercicoo de uma apostila por conta propria…o exercicio é o seguinte Altere os modificadores de acesso (public, private) …alguem pode me dizer ond estao esses modificadores…a apostila ta confusa eu nao etendi… :frowning: :frowning:

import javax.swing.JOptionPane;

public class TimeTest{

public static void main(String args[])

{

Time1 time = new Time1();

String output= The initial universal time is:+

time.toUniversalString()+

"\n The initial standart time is: "+ time.toString() +

“\n Implicit toString() call: + time;
time.setTime(13,27,6);
output += "\n\nUniversal time after setTime is:" +
time.toUniversalString() +
"\n Standard time after setTime is: " +time.toString();

time.setTime(99, 99, 99);
output += "\n\n After attemping invalid settings: " +
"\n Universal time: " + time.toUniversalString() +
"\n Standard time: " + time.toString();

JOptionPane.showMessageDialog(null, output,
"Testing Class Time1",
JOptionPane.INFORMATION_MESSAGE);
	System.exit(0);
}

}


import java.text.DecimalFormat;

public class Time1 extends Object{

public int hour;    ////seria esses os modificadores

public int minute;

public int second;
public Time1(){

setTime(0,0,0);

}
public void setTime (int h, int m, int s){

hour= ((h>=0 &&  h<24) ? h:0);

minute= (( m>= 0 && m<60) ? m:0);

second = (( s>=0 && s<60 ) ? s:0);

}
public String toUniversalString(){

DecimalFormat twoDigits = new DecimalFormat(00);

return twoDigits.format (hour) + “:” +

twoDigits.format(minute) +":" +

twoDigits.format(second);

}
public String toString()

{

DecimalFormat twoDigits = new DecimalFormat(00);

return (( hour == 12|| hour ==0) ? 12 : hour % 12) +

: + twoDigits.format (minute) +

: + twoDigits.format (second) +

(hour <12 ? AM :PM);

}

}

5 Respostas

V

Dica: verifique os atributos da classe Time1

jane_bravo

import java.text.DecimalFormat;

public class Time1 extends Object{

private int hour;

private int minute;///eses sao os atributos q devo mudar?

private int second;
public Time1(){                    ///devo mudar todos?

setTime(0,0,0);

}
public void setTime (int h, int m, int s){

hour= ((h>=0 &&  h<24) ? h:0);

minute= (( m>= 0 && m<60) ? m:0);

second = (( s>=0 && s<60 ) ? s:0);

}
public String toUniversalString(){

DecimalFormat twoDigits = new DecimalFormat(00);

return twoDigits.format (hour) + “:” +

twoDigits.format(minute) +":" +

twoDigits.format(second);

}
public String toString()

{

DecimalFormat twoDigits = new DecimalFormat(00);

return (( hour == 12|| hour ==0) ? 12 : hour % 12) +

: + twoDigits.format (minute) +

: + twoDigits.format (second) +

(hour <12 ? AM :PM);

}

}
W

bom acho que voce ta em algum capitulo sobre modificadores de acesso e ele fala pra tu alterar pra ver os efeitos disso certo?!!

voce perguntou onde tá os modificadores mas já deu a resposta ué!! so olhar o quote acima, so faltou interpretação da tua parte,pra intender o alterar
seria algo do tipo alternar/trocar sacou ?! : D bom como nesse é uma pergunta conceitual não tem porque fazer mistério, onde tá public poderia escrever private no lugar,
o importante e você entender efeito dessa modificação vai causar no teu programa.

flw

jane_bravo

dexa eu v se eu entendi…é so trocar ond esta public por private nessa classe time1,mas quando eu fiz issu nad mais funcionou pq?compilei a classe time1 e ok,mas quando compilei Timetest nao deu certo…e quando rodei Time1 nem funcionou…alguem sabe pq eu nao to entendo isso d encapsulamento e efeito colateral… :frowning: :frowning: :frowning:

import java.text.DecimalFormat;

private class Time1 extends Object{

private int hour;

private int minute;

private int second;

private Time1(){

setTime(0,0,0);

}

private void setTime (int h, int m, int s){

hour= ((h>=0 &&  h<24) ? h:0);

minute= (( m>= 0 && m<60) ? m:0);

second = (( s>=0 && s<60 ) ? s:0);

}
private String toUniversalString(){

DecimalFormat twoDigits = new DecimalFormat(00);

return twoDigits.format (hour) + : +

twoDigits.format(minute) +":" +

twoDigits.format(second);

}
private String toString()

{

DecimalFormat twoDigits = new DecimalFormat(00);

return (( hour == 12|| hour ==0) ? 12 : hour % 12) +

: + twoDigits.format (minute) +

: + twoDigits.format (second) +

(hour <12 ? AM :PM);

}

}
jane_bravo

sera q alguem pode me dizer se é issu msm q eu tenhu q faze :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :cry: :cry: :cry:

Criado 4 de junho de 2008
Ultima resposta 5 de jun. de 2008
Respostas 5
Participantes 3