PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 19, 2022

[FIXED] Why are the variables I'm requesting showing up as 0.0 when called from another class?

 May 19, 2022     class, java, object, variables     No comments   

Issue

So basically my program is a StudySchedule where it takes user input (StudyTime, Subjects, PrioritizedSubjects, PriorityScale) and then creates a schedule based off each of those values. It begins with a class CreateSchedule which takes all the user input and then my other class CalculateScheduleTime takes the input and makes calculations (Calculate does extend to Create).

But when I request the variables from CreateSchedule, to CalculateScheduleTime, the variables appear as 0.0 rather than the number I had put in.


class CreateSchedule extends Test {

    public String ScheduleName;
    public double userStudyTime;
    public double userSubjects;
    public double userPrioritySubjects;
    public double userSubjectScale;

    public String getScheduleName() {
        ScheduleName = setScheduleName();
        return (ScheduleName);
    }

    public double getuserStudyTime() {
        userStudyTime = setuserStudyTime();
        return (userStudyTime);
    }

    public double getuserSubjects() {
        userSubjects = setuserSubjects();
        return (userSubjects);
    }

    public double getuserPrioritySubjects() {
        userPrioritySubjects = setuserPrioritySubjects();
        return (userPrioritySubjects);
    }

    public double getuserPriorityScale() {
        userSubjectScale = setuserPriorityScale();
        return (userSubjectScale);
    }

    public static String setScheduleName(){
        System.out.println("What would you like to name your Schedule?");
        Scanner sch = new Scanner(System.in);
        return sch.nextLine();
    }

    public static double setuserStudyTime(){
        System.out.println("How many hours are you studying for?");
        Scanner sch = new Scanner(System.in);
        return sch.nextDouble();
    }

    public static double setuserSubjects (){
        System.out.println("How many subjects are you studying?");
        Scanner sch = new Scanner(System.in);
        return sch.nextDouble();
    }

    public static double setuserPrioritySubjects (){
        System.out.println("How many subjects are you prioritizing?");
        Scanner sch = new Scanner(System.in);
        return sch.nextDouble();
    }

    public static double setuserPriorityScale (){
        System.out.println("On a scale of 1 - 5, how much priority would you like to give the prioritized subjects?");
        Scanner sch = new Scanner(System.in);
        return sch.nextDouble();
    }

    public double confirm() {
        System.out.println("Input Results:");
        System.out.println("Schedule Name: " + ScheduleName);
        System.out.println("Study Time: " + userStudyTime);
        System.out.println("Subjects: " + userSubjects);
        System.out.println("Priority Subjects: " + userPrioritySubjects);
        System.out.println("Priority Scale" + userSubjectScale);
        return (0);
    }

}

class CalculateScheduleTime extends CreateSchedule {

    public double SubjectPriorityTime;
    public double SubjectRemainderTime;

    public CalculateScheduleTime() {

    }

    public double calcSubjectPriorityTime() {
        System.out.println("Priority" + userSubjectScale);
        double PriorityPercent = ((double) (userSubjectScale / 5.0));
        System.out.println(userSubjectScale);
        SubjectPriorityTime = ((double) (PriorityPercent * userStudyTime));
        System.out.println("Time to Prioritized Subject is: " + SubjectPriorityTime);
        return (SubjectPriorityTime);
    }

    public double calcSubjectRemainderTime() {
        System.out.println("Remainder");
        SubjectRemainderTime = ((double) (SubjectPriorityTime - userStudyTime));
        System.out.println("Remainder time to Subject is: " + SubjectRemainderTime);
        return (SubjectRemainderTime);
    }
}

public class Test {
    public static void main(String[] args) {
        CreateSchedule user = new CreateSchedule();
        user.getScheduleName();
        user.getuserStudyTime();
        user.getuserSubjects();
        user.getuserPrioritySubjects();
        user.getuserPriorityScale();
        user.confirm();

        CalculateScheduleTime calc = new CalculateScheduleTime();
        calc.calcSubjectPriorityTime();
        calc.calcSubjectRemainderTime();
    }
}

Solution

That's not what subclassing is for.

A class describes what a given instance can do. "Dog" is a class. "Lassie" is an instance of it.

Subclassing is a thing you do to concepts, not instances. You might for example have a class "Animal", and a subclass "Dog": Dog simply specializes Animal: Any Dog is also an Animal and therefore can do and has all the properties that all Animals have, and perhaps a few additional things and properties (in java parlance: It would have all fields and methods of the superclass, and can add more. It cannot remove any).

When you write CreateSchedule user = new CreateSchedule(); that's like writing: Dog rover = new Dog(); - then you write CalculateScheduleTime calc = new CalculateScheduleTime(); which is like writing GermanSchnauser fifi = new GermanSchauser();.

You made a whole new dog, which gets its own copy of all those fields, which are all still 0 - uninitialized.

The 'calc' stuff should just go in CreateSchedule, probably. But there is a ton wrong with this code:

  • Classes represent tangible concepts. A good class name is 'Schedule'. Or perhaps 'ScheduleCreator'. 'CreateSchedule' is not a proper class name, and thinking about it as 'the code for creating schedules' is just plain incorrect, the right way to think about classes is about what they represent. "It is the code that explains what Schedules can do and how to create them", that's a proper way to think about it (and that class would be called Schedule. Not CreateSchedule).
  • methods named getX should not have sideeffects.
  • You should not be making a new scanner every time.
  • setters take an argument. They don't ask the user.
  • Separate concerns. A Schedule should just do schedule stuff - something else should be doing the interaction with the user. Either a main method or a class for this (perhaps TextBasedScheduleCreator).
  • Those calcX() methods store the result in a field, return it, and that field is never actually used anywhere. Just return it, don't have that field.


Answered By - rzwitserloot
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing