Method not calculating properly

Thanks, but telling us how add doesnt really help.
The OP is clearly trying to work out how to write and use methods, not trying to find the most efficient way to add some numbers together.
You did say this: 'I would write those in the following way'. 'Would you really?' was my point. If you're giving examples from a teaching position then give examples of good code.
 
You did say this: 'I would write those in the following way'. 'Would you really?'

I said I would write them in that way to avoid confusion.
It looks to me like he was a little confused with the variable names in his main method and how they are separate from the variable names in the other methods so I renamed them to make it clearer.
 
Cheers for all the help,much appreciated! I had too take a step back from that program I was trying too write last as I just confused myself :confused: I'll come back too it again.

Here's a little program I wrote today which compares strings

Code:
string[] name=new string[5];

            name[0] = "kevin";
            name[1] = "bill";
            name[2] = "Ken";
            name[3] = "Bob";
            name[4] = "Jo";
           

            Console.WriteLine("Please enter your name: ");
            string input = Console.ReadLine();

            if (input.Equals("kevin"))
            {
                Console.WriteLine("Helllooo KEV MON!");
            }
            else
            {
                Console.WriteLine("WRONG NAME DUDE!");
            }

            Console.WriteLine("Please enter another name:");
            string input2 = Console.ReadLine();

            if (input2.Equals("bill"))
            {
                Console.WriteLine("Helllloooooooo Billy Boy");
            }
            else
            {
                Console.WriteLine("CRAPPY NAME DUDE!");
            }

Nothing too exciting LOL! but at least it works, its not finished btw.
 
I mean did you actually do any proper debugging or are you perpetually guessing without observing. I think your response is pretty damning of whatever course you're doing if you don't know what 'attach a debugger' means. Debugging is essential to programming - learn to do it asap!

Our tutor never mentioned "debugging" when we would run our programs we were told too click "debug,run without debugging"
 
Could check all names at once.

Code:
            List<string> name = new List<string>();

            name.Add("kevin");
            name.Add("bill");
            name.Add("ken");
            name.Add("bob");
            name.Add("jo");

            Console.WriteLine("Please enter your name: ");
            string input = Console.ReadLine();

            if (name.Contains(input.ToLower()))
            {
                Console.WriteLine($"Hi {input}");
            }
            else
            {
                Console.WriteLine("Name not found");
            }
 
Sorry, dude, your course aint worth a ****.

Valuable input to the thread :rolleyes:

For beginners, it can be more effective to learn what you're doing if you dont use the debugger. If his program crashes or doesnt do what he expects, he's going to have to go through each line and think about what it does and which one could be causing the problem.

Of course, he will have to learn how to debug eventually.
 
Last edited:
It is actually, because he can complain to his lecturer/teacher/... that he's not being taught essentials. If no one says how bad his course is then he'll just accept it.
 
Well, debugging wasn't the first thing I learned. Guess my computer science degree aint worth **** either.

In fact, I just looked up MIT OpenCourseWare and they don't cover debugging until lecture 7, guess a degree from MIT aint worth **** either.
 
Well, debugging wasn't the first thing I learned. Guess my computer science degree aint worth **** either.

In fact, I just looked up MIT OpenCourseWare and they don't cover debugging until lecture 7, guess a degree from MIT aint worth **** either.
Didn't learn debugging at all throughout my CS degree too, not sure why learning how to use a debugger is THAT important that you can judge a course on whether it's included or not.

Of course I use one daily these days, but certainly when I was learning (and writing the small programs OP is writing), I think it helped me try to understand what each line is doing and then sticking the odd print statement in to see state.
 
In my 15 year career of professional software development, the amount of debugging I do has declined drastically. The last time I actually used a debugger was weeks, if not months, ago. Tests and keeping things properly encapsulated and separate erodes any need to debug.
 
I passed my course :) and will be starting a mobile technology course in January :).

Seeing as we'll be using Java for that course, I decided to start messing around with it a bit using eclipse. I'm confused about something though, "public static class Hero "why do I have to put in the static? If I don't put it in Warrior Kevin = new Warrior(); gets an error.

Code:
public class Inheritence5 {

    public static class Hero
    {
        protected String Name;
        protected int Age;
        protected String Class;
        protected String Vocation;
        protected int Strenght;
        protected int DamageOutPut;
        protected int HitPoints;
        protected int DefensePoints;
        protected int Intelligence;
        protected int Focus;
        protected Boolean Evil;
        protected Boolean Good;
    
    public  void HeroStats()
    {
         System.out.println("Name: " +Name);
         System.out.println("Age: " +Age);
         System.out.println("Class " + Class);
         System.out.println("Vocation: " +Vocation);
         System.out.println("Strenght: " +Strenght);
         System.out.println("Damage Output: " +DamageOutPut);
         System.out.println("HitPoints: " +HitPoints);
         System.out.println("DefensePoints: " +DefensePoints);
         System.out.println("Focus: " +Focus);
         System.out.println("Evil: " +Evil);
         System.out.println("Good: " +Good);
    }
    
    }
    
    public  static class Warrior extends Hero
    {
         public int Stamina;
         public double CriticalStrike;
        
         public void SpecialStat()
         {
             System.out.println("Stamina: " +Stamina);
             System.out.println("CriticalStrike" +CriticalStrike);
         }
    }
    
    public static class Wizard extends Hero
    {
        protected int MagicalPoints;
        
        
        protected void SpecialStats()
        {
            System.out.println("MagicalPoints: " +MagicalPoints);
            
        }
    }
    
    
    public static void main(String[] args)
    {
        Warrior Kevin = new Warrior();
        Kevin.Name = "Kevin";
        Kevin.Age = 31;
        Kevin.Class = "Warrior";
        Kevin.Vocation = "Tank";
        Kevin.Strenght = 90;
        Kevin.DamageOutPut = 200;
        Kevin.HitPoints = 350;
        Kevin.DefensePoints = 100;
        Kevin.Intelligence = 50;
        Kevin.Focus = 56;
        Kevin.Evil = false;
        Kevin.Good = true;
        Kevin.Stamina = 78;
        Kevin.CriticalStrike = 77.5;
        Kevin.HeroStats();
        Kevin.SpecialStat();
      
        System.out.println();
        
         Wizard Conor = new Wizard();
         Conor.Name = "Conor";
         Conor.Age = 32;
         Conor.Class = "Wizard";
         Conor.Vocation = "Healer";
         Conor.Strenght = 40;
         Conor.DamageOutPut = 300;
         Conor.HitPoints = 500;
         Conor.DefensePoints = 100;
         Conor.Intelligence = 200;
         Conor.Focus = 80;
         Conor.Evil = false;
         Conor.Good = true;
         Conor.MagicalPoints = 500;
         Conor.HeroStats();
         Conor.SpecialStats();
        
         System.out.println();

I wrote more or less the same program in visual studio with c# and didn't have this problem at all with static, Is there a way to fix it?
 
I'm confused about something though, "public static class Hero "why do I have to put in the static? If I don't put it in Warrior Kevin = new Warrior(); gets an error.

Because you are declaring the Hero class as local to the Inheritance5 class. If you want it to be a 'full' class define it in its own Hero.java file. One file = one class is the general rule in java.
 
Why won't this calculate tax payed please anyone? it just prints out 0, I was testing it out on David O Sullivan first just too see if it would work, hence why it not in with the others.


Code:
public class Accountancy {

  
    public static void main(String[] args) {
        
        Customer a1 = new Customer();
        Customer a2 = new Customer();
        Customer a3 = new Customer();
        
        a1.CustomerName = "Kevin O'Sullivan";
        a1.OpeningBalance = 20000;
        a1.DebitPurchases = 5000;
        a1.CreditPayment = 2000;
        a1.ClosingBalance = 0;
        a1.HoursWorked = 40.5;
        a1.HourlyRate = 20.0;
        a1.HighTaxRate = 42.0;
        a1.TotalWage = 0;
        
        System.out.println("Your name is: " +a1.CustomerName);
        System.out.println("Your opening Balance is: " +a1.OpeningBalance);
        System.out.println("Your closing Balance now is: " +a1.CalculateCustomerBalance(a1.OpeningBalance,a1.CreditPayment,a1.DebitPurchases,a1.ClosingBalance));
        System.out.println("This Week you Worked: " +a1.HoursWorked);
        System.out.println("Your Hourly Rate is: " +a1.HourlyRate);
        System.out.println("You pay the tax rate of 42%!");
        System.out.println("Your total wage this week is: " +a1.WeeklyWage(a1.HourlyRate,a1.HoursWorked,a1.TotalWage));
        System.out.println();
        
    
        a2.CustomerName = "Jerry O'Sullivan";
        a2.OpeningBalance = 50000;
        a2.DebitPurchases = 10000;
        a2.CreditPayment = 5000;
        a2.ClosingBalance = 0;
        a2.HoursWorked = 56.5;
        a2.HourlyRate = 30.0;
        a2.HighTaxRate = 42.0;
        a2.TotalWage = 0;
        
        System.out.println("Your name is: " +a2.CustomerName);
        System.out.println("Your opening Balance is: " +a2.OpeningBalance);
        System.out.println("Your Closing Balance is: " +a2.CalculateCustomerBalance(a2.OpeningBalance,a2.CreditPayment,a2.DebitPurchases,a2.ClosingBalance));
        System.out.println("This Week you Worked: " +a2.HoursWorked);
        System.out.println("Your Hourly Rate is: " +a2.HourlyRate);
        System.out.println("You pay the tax rate of 42%!");
        System.out.println("Your total wage this week is: " +a2.WeeklyWage(a2.HoursWorked,a2.HourlyRate,a2.TotalWage));
        System.out.println();
        
        
        a3.CustomerName = "David O'Sullivan";
        a3.OpeningBalance = 700;
        a3.DebitPurchases = 500;
        a3.CreditPayment = 100;
        a3.ClosingBalance = 0;
        a3.HoursWorked = 65.4;
        a3.HourlyRate = 15.0;
        a3.HighTaxRate = 42.0;
        a3.TotalTaxPayed = 0;
        a3.TotalWage = 0;
        
        System.out.println("Your name is: " +a3.CustomerName);
        System.out.println("Your opening Balance is: " +a3.OpeningBalance);
        System.out.println("Your Closing Balance is: " +a3.CalculateCustomerBalance(a3.OpeningBalance,a3.CreditPayment,a3.DebitPurchases,a3.ClosingBalance));
        System.out.println("This week you worked: " +a3.HoursWorked);
        System.out.println("Your Hourly Rate is: " +a3.HourlyRate);
        System.out.println("You pay the tax rate of 42%!");
        System.out.println("Your total wage this week is: " +a3.WeeklyWage(a3.HoursWorked,a3.HourlyRate,a3.TotalWage));
        System.out.println("The amount of tax you pay weekly is: " +a3.TaxCalculator(a3.TotalTaxPayed,a3.TotalWage,a3.HighTaxRate));
        
        System.out.println();


Code:
public class Customer {
 
    protected String CustomerName;
    protected double OpeningBalance;
    protected double DebitPurchases;
    protected double CreditPayment;
    protected double ClosingBalance;
    protected double HoursWorked;
    protected double HourlyRate;
    protected double TotalWage;
    protected double HighTaxRate;
    protected double TotalTaxPayed;
    
    
    
    public double CalculateCustomerBalance(double OpeningBalance,double CreditPayment,double DebitPurchases,double ClosingBalance)
    {
        return ClosingBalance = OpeningBalance + CreditPayment - DebitPurchases;
    
    }
    
    public double WeeklyWage(double HourlyRate,double HoursWorked,double TotalWage)
  {
   return TotalWage =  HoursWorked * HourlyRate;

  }

 public double TaxCalculator(double TotalWage,double HighTaxRate,double TotalTaxPayed)
 {
     return TotalTaxPayed = TotalWage * HighTaxRate;
 }
 
Your functions in the customer class shouldn't be returning
TotalTaxPayed = ... It should just return the result of the calculation directly, e.g. for your tax calculator:

return TotalWage * HighTaxRate;

In addition, it doesn't make sense for you to pass in the parameters to those functions because you have already assigned the values as properties on your class. In the functions you can just reference them with this.HighTaxRate. In your tax calculator function the TotalWage property can then be replaced with a call to the WeeklyWage function.
 
Your functions in the customer class shouldn't be returning
TotalTaxPayed = ... It should just return the result of the calculation directly, e.g. for your tax calculator:

return TotalWage * HighTaxRate;

In addition, it doesn't make sense for you to pass in the parameters to those functions because you have already assigned the values as properties on your class. In the functions you can just reference them with this.HighTaxRate. In your tax calculator function the TotalWage property can then be replaced with a call to the WeeklyWage function.

The functions should be public void then instead of public double ?
 
No, a void would mean it doesn't return anything, it will still be returning the result of your calculation. You shouldn't be assigning to a variable in your return statement. Either assign to a variable and then return it, or just return the result of the calculation directly.
 
Back
Top Bottom