• Post Reply Bookmark Topic Watch Topic
  • New Topic

Gas law calculator  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I just started on a new calculator, here it is, let me know how else you guys might write it.

 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A switch block can have more than one case statement:

Or you could write "solv = sc.next().toLowerCase()"
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really want feedback? Sorry, but it ain't going to be nice.

I don't like the name of the package, nor either of the packages. PVNRT is incomprehensible unless you already know it is a Boyle's law calculator. chemCalculator should start with C not c.
You have a method which does not access any instance fields and it is not static. Actually I think it would be better for those variables to be fields.
Don't declare several things in one line.
R is a constant: make it static and final and initialise it immediately on declaration. It would be appropriate for it to be a global constant, in which case it can have public access, but might be better in a class of its own.
The method is far too long. There is a lot of repeated code in that switch. You can simplify the switch greatly with a bit of fall‑throughDon't put lots of code inside cases: put method calls instead, as far as possible. Get the cases short.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you really want feedback? Sorry, but it ain't going to be nice.

I don't like the name of the package, nor either of the packages. PVNRT is incomprehensible unless you already know it is a Boyle's law calculator. chemCalculator should start with C not c.
You have a method which does not access any instance fields and it is not static. Actually I think it would be better for those variables to be fields.
Don't declare several things in one line.
R is a constant: make it static and final and initialise it immediately on declaration. It would be appropriate for it to be a global constant, in which case it can have public access, but might be better in a class of its own.
The method is far too long. There is a lot of repeated code in that switch. You can simplify the switch greatly with a bit of fall‑throughDon't put lots of code inside cases: put method calls instead, as far as possible. Get the cases short.


Thanks for the honesty, I actually get quite annoyed when people are afraid to be honest because it might offend you. Secondly, what would you suggest the packages be named? IdealLaw? And are you suggesting I create methods to call inside the case? And how exactly do I use global and what does it do? Sorry for my lack of knowledge, I am still relatively new to Java and most other programming.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know the best name for your package: if it contains calculators maybe gas or calculator. Use lower‑case throughout and look through the Java Tutorials about their strange naming conventions.
A global constant can be inside a public classAnd you would call it as Temperatures.ABSOLUTE_ZERO (or might use static import).
Yes, I think you should create methods to call from the cases. As a rule of thumb, two small methods are usually better than one large one.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the corrected version of it, let me know if I screwed anything else up please. Also, if you have any suggestions for when I am writing the executive class, feel free to let me know.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I just barely started to write the executive script and managed to screw something up haha, its saying that my variables aren't initialized (error on line 40). Any help would be greatly appreciated.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I don't actually want to print out the information, but I just did that to check to make sure that the variables were available from the execution method.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still lots of repeated code, particularly for entering values. I don't like the single‑letter method names.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a way to get rid of the repeated code without making the user enter in the variable they are salvoing for as null
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And do you see a way to fix the error on line 40?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorgen Bech wrote:And do you see a way to fix the error on line 40?

How many P, V, N and T variables have you got in your code ?
Which variables are you passing to the method on line 40 ?
Which variables are you initialising in your T method ?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorgen Bech wrote:I don't see a way to get rid of the repeated code . . .
Repeated code ⇔ divide into separate methods.

Code likely to be useful elsewhere ⇔ divide into separate classes.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Jorgen Bech wrote:And do you see a way to fix the error on line 40?

How many P, V, N and T variables have you got in your code ?
Which variables are you passing to the method on line 40 ?
Which variables are you initialising in your T method ?


I fixed that right after I posted my code. I simply put


But I don't remember how to use them in another class. Would I still have to pass them through the parameter of the method I am using the variables in?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorgen Bech wrote:I fixed that right after I posted my code.

You might have got rid of the error message, but you haven't fixed your problem. Think about the third question I asked.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would possibly write one method that goes something like this


So you have one method that could get all four, and it simply skips over the one you don't need/want to solve for.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the new code, I don't know if it works or not yet because I am currently working off of a Chromebook (don't hate, school requires it). So if you see anything that might turn up an error, please let me know, thanks.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been thinking about it and I probably am going to just make Execution a method in the class ChemCalculator to save myself some trouble, anybody think that its a terrible idea?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a load of code which looks as if it won't work.
I think we need to go back to first principles. You want to enter three out of four of npvt and have the fourth worked out. The only class which will actually work is the Constants class.
Back to the beginning: Write a Calculator class with the four fields, and give it a toString method. At this stage you will have it printing 0 for everything.
Give it enterP(double p) methods, and similar. Give it calculateP() methods and similar. Test it like this:-
 
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need 3 out of 4 variables, all 4 are the same type. So write a single method that is invoked like

You could even get fancy and have getValue() expand P to "Pressure", V to "Volume", etc.

If you're going to use command line arguments you should google "java getopt".

 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woah, remind me never to write code without being able to test it again. I'm making all necessary fixes in Netbeans now
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a dumb question, why should I not leave it like this: (it runs without any errors)

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is everything static?
That looks so different from the previous attempt, however. It does look like something that actually works!
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why is everything static?
That looks so different from the previous attempt, however. It does look like something that actually works!

Nice! Haha. And I usually make everything static because I don't like making objects to call classes, I don't see the point when you can just write static to eliminate the need for it.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is an object language. You are supposed to create objects.
[edit]But it does look like code which works [/edit]
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question before I write the class Execution, then show you guys it, and find out I managed to do the exact opposite of what I wanted; Should I just do it like:

Or should I make several methods to call from the cases?
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Java is an object language. You are supposed to create objects.
[edit]But it does look like code which works [/edit]

I get that point, but, correct me if I'm wrong, isn't using static a faster, simpler, and more effective way to summon methods? Because that's what it seems like to me. I'm not trying to pick an argument with you, I'm just trying to understand the true purpose of using Objects when there appears to be a better way of accomplishing the same task.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am almost done! I just need to implement a significant figure filter (easier said than done I know). Here is my code, please let me know if there is a way to make it more effective

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think your GetP(), GetV(), etc should all call a common getUserInput() method. When you have 2 or more identical lines That's Often A Bad Sign (tm).

Look at it this way. If you decide to change P to a float you currently have to change several parts of the code to read it correctly. My way you change 1. This is neither a Java nor an OO issue, it's good programming practice.

The correct way to do it is along the lines of



Ok, so you slipped some good changes in from the time I started my reply until I finally accepted it. Your latest looks good.

 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim Venolia wrote:I still think your GetP(), GetV(), etc should all call a common getUserInput() method. When you have 2 or more identical lines That's Often A Bad Sign (tm).

Look at it this way. If you decide to change P to a float you currently have to change several parts of the code to read it correctly. My way you change 1. This is neither a Java nor an OO issue, it's good programming practice.

The correct way to do it is along the lines of



Ok, so you slipped some good changes in from the time I started my reply until I finally accepted it. Your latest looks good.


Sorry, I read your suggestion and wanted to use it but I don't know how to implement it. How would I set the variables in the GetUserInput class to other variables?
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I know they'res people with a lot more experience with java than me here, so I was wondering if most programs start off pretty good, and then minor changes are made at the end, or is it usually kind of like the way I corrected mine?
EDIT: Also, how should I best go about finding, between three doubles, the one with the greatest number of characters (numbers) inside it? The way I think I should approach it is:


and then execute a series of if conditions testing the value of the indexes of other variables.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use floats.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use floats.

I don't plan on it, I think he was just using that as an example.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I read your suggestion and wanted to use it but I don't know how to implement it. How would I set the variables in the GetUserInput class to other variables?

Something like the following syntax-challenged code (still on my first cup of coffee)

Aargh! Lets pretend my columns line up above, I need coffee more than I need to fix it in this submission pane. Besides, I like your final way of doing it better than mine.

Hey, I know they'res people with a lot more experience with java than me here, so I was wondering if most programs start off pretty good, and then minor changes are made at the end, or is it usually kind of like the way I corrected mine?

IMHO it doesn't matter, what kills programs are changing requirements and tight deadlines. Never underestimate Management's/Marketing's tendency to decide that, say, requiring 2 wings per bird is too costly and setting a 1 wing limit. No, we can't afford to move the deadline. It should be easier to code, you've got fewer wings to worry about.

Cynicism aside, when you're learning a language what counts is getting it to work. Once you're comfortable with the language then you should put more effort in design (even if it's just thinking in the shower) before ever writing a single line.



 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aargh! Lets pretend my columns line up above, I need coffee more than I need to fix it in this submission pane. Besides, I like your final way of doing it better than mine.


It's alright, effort counts haha, and I get what I would do. I was just wondering in case I need to do it that way in a different program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!