'Having trouble creating overloaded constructor, instantiating an object

I'm new with Java and I'm having trouble going along with my teacher's video lectures (I'm taking an online class), and I'm having trouble with this assignment:

Create a FitnessTracker class that includes data fields for a fitness activity, the number of minutes spent participating, calories used, and the date. The class includes methods to get each field. In addition, create a default constructor that automatically sets the activity to “running,” the minutes to 0, calories used to 0, and the date to January 1 of the current year. Save the file as FitnessTracker.java. Create an application that demonstrates each method works correctly, and save it as TestFitnessTracker.java.

b. Create an additional overloaded constructor for the Fitness Tracker class you created in Exercise 3a. This constructor receives parameters for each of the data fields and assigns them appropriately. Add any needed statements to the TestFitnessTracker application to ensure that the overloaded constructor works correctly, save it, and then test it.

c. Modify the Fitness Tracker class so that the default constructor calls the four-parameter constructor. Save the class as FitnessTracker2.java. Create an application to test the new version of the class, and name it TestFitnessTracker2.java.

I've also been watching some youtube videos and they have helped with some understanding, but I'm still not entirely getting it. I keep getting an error "Illegal parameter for modifier day, only final is permitted" next to the "date method," among other errors as well. Here's what I have so far, but I'm kind of lost at this point and don't know how to move forward:

package javaProgramming;

import java.util.Scanner;

public class fitnessTracker {
    public static void main(String[] args){ 
       tracker trackerObject = new tracker("January 1st", "running", 0, 0);
    }
}

class tracker {
    public tracker(String date, String active, int mins, int cal){
       Scanner input = new Scanner(System.in);
    
       System.out.println("What day are you recording this activity for?");
       public String day = input.nextLine();
    
       System.out.println("What fitness exersise did you complete?");
       public String active = input.nextLine();

       System.out.println("How many minutes did you spend " + active + "?");
       public int mins = input.nextInt();
    
       System.out.println("How many calories did you burn?");
       public int cal = input.nextInt();
    }
            
    public static void date(String day){
           System.out.println("I did this on " + day + ".");
        
    public static void activity(String active){
        System.out.println("Today I plan on " + active);
    }
    
    public static void minutesSpent(int mins){
        System.out.println("I spent " + mins + " minutes doing so.");
    }
    
    public static void caloriesUsed(int cal){
        System.out.println("I burned " + cal + " calories.");
    }
}

It would be greatly appreciated if someone could help me out with this!



Solution 1:[1]

you can't use public inside a function, only in the class context:

public String day = input.nextLine();

either remove public

String day = input.nextLine();

or move the declaration of the variable to the class:

class tracker{
    public String day;
    public tracker(String date, String active, int mins, int cal){
        day = input.nextLine();

Solution 2:[2]

Try this.

class 1 (contains main method)

package javaProgramming;

import java.util.Scanner;

public class FitnessTracker {

    public static void main(String[] args){ 
        Tracker trackerObject = new Tracker("January 1st", "running", 0, 0);
        Scanner input = new Scanner(System.in);

        String day;
        String active;
        int mins;
        int cal;


        System.out.println("What day are you recording this activity for?");
        day = input.nextLine();

        System.out.println("What fitness exersise did you complete?");
        active = input.nextLine();

        System.out.println("How many minutes did you spend " + active + "?");
        mins = input.nextInt();

        System.out.println("How many calories did you burn?");
        cal = input.nextInt();

        trackerObject.date(day);
        trackerObject.activity(active);
        trackerObject.minutesSpent(mins);
        trackerObject.caloriesUsed(cal);
    }
}

class 2

package javaProgramming;

class Tracker {

    private String day;
    private String active;
    private int mins;
    private int cal;

    public Tracker(String date, String active, int mins, int cal){
        this.day = date;
        this.active = active;
        this.mins = mins;
        this.cal = cal;     
    }

    public void date(String day){
        System.out.println("I did this on " + day + ".");
    }
    public void activity(String active){
        System.out.println("Today I plan on " + active);
    }

    public void minutesSpent(int mins){
        System.out.println("I spent " + mins + " minutes doing so.");
    }

    public void caloriesUsed(int cal){
        System.out.println("I burned " + cal + " calories.");
    }
}

My editions :

  1. class name by convention starts with a capital
  2. Scanner input only happens in main method to pass the user input value to the Tracker class so that the constructor can initialize the values to each variables. The set values will be passed to each parameter of date, activity, minuteSpent, caloriesUsed methods within the class.

This is the output in my console:

What day are you recording this activity for?
3
What fitness exersise did you complete?
squat
How many minutes did you spend squat?
5
How many calories did you burn?
100
I did this on 3.
Today I plan on squat
I spent 5 minutes doing so.
I burned 100 calories.

I hope this helped!

Solution 3:[3]

THIS WILL SERVE AS YOUR class for overloaded constructors:

    import java.time.*;
    public class FitnessTracker2 {
        String activity;
        int minutes;
        LocalDate date;
        public FitnessTracker2() {
            //By using this key word we can call overloaded constructor from default constructor
    //this is the modification for exercise 3b
    this("running",0,LocalDate.of(2018, 1, 1));
    
        }
        public FitnessTracker2(String a, int m, LocalDate d) {
            this.activity = a;
            this.minutes = m;
            this.date = d;
    
        }
        public String getActivity() {
            return activity;
        }
        public int getMinutes() {
            return minutes;
    
        }
        public LocalDate getDate() {
            return date;
        }
    }

THIS WILL SERVE AS YOUR main class:

import java.time.*;
public class TestFitnessTracker2
{
   public static void main(String[] args)
   { 
      FitnessTracker2 exercise3 = new FitnessTracker2();

      System.out.println(exercise3.getActivity() + " " + exercise3.getMinutes() +
         " minutes on " + exercise3.getDate());

      // code to test constructor added for exercise 3b
 
      LocalDate date = LocalDate.of(2020, 8, 20);
      FitnessTracker2 exercise2 = new FitnessTracker2("bicycling", 35, date);

      System.out.println(exercise2.getActivity() + " " + exercise2.getMinutes() +
         " minutes on " + exercise2.getDate());
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gavriel
Solution 2 Bohemian
Solution 3 R.E.F.