Question:

Help with java classes!! I cannot make my program work!!!?

by Guest56168  |  earlier

0 LIKES UnLike

Here's the class I want to make:

import java.util.*;

public class ClaseFecha {

//declaracion de variables

String dia, mes;

public void SetDia(String d)

{

dia=d;

}

public String getDia() {

return dia;

}

public void setMes(String m) {

mes=m;

}

public String getMes() {

return mes;

}

public String toString() {

return dia+"-"+mes;

}

}

Here is the main:

import java.util.*;

public class Fecha {

public static Scanner tecla=new Scanner(System.in);

public static void main(String[] args) {

//Crea el objeto con el metodo constructor.

Fecha f=new Fecha();

String d,m;

m="0";

d="0";

d.setDia("06");

m.setMes("10");

System.out.println("La fecha de hoy es: "+f.toString());

//modificar

System.out.print("Qué día es hoy (dd)");

d=tecla.nextLine();

d.setDia(d);

System.out.print("Qué mes es hoy (mm)");

m=tecla.nextLine();

m.setMes(m);

System.out.println("Tu Fecha modificada: "+f.toString());

}

}

I get this errors:

Fecha.java:19: cannot find symbol

symbol : method setDia(java.lang.String)

location: class java.lang.String

d.setDia("06");

^

Fecha.java:20: cannot find symbol

symbol : method setMes(java.lang.String)

location: class java.lang.String

m.setMes("10");

^

Fecha.java:26: cannot find symbol

symbol : method setDia(java.lang.String)

location: class java.lang.String

d.setDia(d);

^

Fecha.java:29: cannot find symbol

symbol : method setMes(java.lang.String)

location: class java.lang.String

m.setMes(m);

^

4 errors

 Tags:

   Report

1 ANSWERS


  1. You are declaring d and m as Strings and start by setting them equal to "0". That is fine. But then you are trying to use methods as if they were ClaseFecha objects. If you want to use the setDia, getDia, setMes, getMes methods, you need to declare d and m as new ClaseFecha objects. Also, when you do that, you will not be able to use the d = "0" and m = "0" statements. But that is what the setDia and setMes methods are for, if I understand your code.

    Your errors are because you are declaring variables as one type, but trying to use methods as if they were a different object type.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.