I am trying to write a simple program that has two classes. I want one class (with the main method) to handle all the input and output and the other class to handle all of the mathematics then return the calculations to the main method. I can successfully pass variables from main method to an object in the mathematics class and have tested the results in that method with a println but can't seem to pass the finished calculations back to my main method. Here is my code, please help me understand. Thank you very much
Here is class with main method
import java.util.Scanner;
public class io {
public static void main (String[] args){
Scanner chargeTankStartGaugeFeetInput = new Scanner(System.in);
Scanner chargeTankStartGaugeInchesInput = new Scanner(System.in);
System.out.println("What is the charge tank's start gauge feet: ");
String chargeTankStartGaugeFeet = chargeTankStartGaugeFeetInput.nextLine();
System.out.println("What is the charge tank's start gauge inches: ");
String chargeTankStartGaugeInches = chargeTankStartGaugeInchesInput.nextLine();
math mathObject = new math();
mathObject.changeGaugesToInches(chargeTankStartGaugeFeet,
chargeTankStartGaugeInches);
System.out.println(mathObject.totalInches(totalInches)
+ " is total inches in io");
}
}
I get an error that says "totalInches" in the main method cannot be resolved to a variable. Is my thinking even close as to how this is supposed to work?
And here is the math class
public class math {
public void changeGaugesToInches(String arg1, String arg2) {
double arg1Double = Double.valueOf(arg1).doubleValue();
double arg2Double = Double.valueOf(arg2).doubleValue();
double totalInches = arg1Double * 12 + arg2Double;
System.out.println(totalInches + " is the total inches");
}
}
You can return the value from the method...
public double changeGaugesToInches(...)
{
....
return totalIncehs;
}
Firstly, by convention, all class and enum names in java should begin with a capital letter. Secondly, you may want to use a more descriptive name for your math class, "UnitsConverter" if that is all that it does.
In changeGaugesToInches, you should rename arg1 and arg2 to feet and inches.
Most importantly, you need to change the method to return the result, and assign it to a variable in your main method:
double totalInches = mathObject.changeGaugesToInches(chargeTankStartGaugeFeet, chargeTankStartGaugeInches);
public double changeGaugesToInches(String arg1, String arg2){
//...
return totalInches;
}
Because this method does not use any instance variables, unless you think you might over ride this method in a subclass (to add metric units, for example) the code would be more efficient if you declared it as static. Also, you can probably use integers unless you require more accuracy.
double totalInches = UnitsConverter.changeGaugesToInches(chargeTankStartGaugeFeet, chargeTankStartGaugeInches);
public static int changeGaugesToInches(String feet, String inches){
return changeGaugesToInches( Integer.parseInt(feet), Integer.parseInt(inches) );
}
// this method can be used more efficiently from parts of your app that already have the units as integers.
public static int changeGaugesToInches(int feet, int in
//...
return totalInches;
}
Any void method can't have return value. Since,
public void changeGaugesToInches(String arg1, String arg2) is a void method therefore it has no return type.
If you make it static then you can't use math mathObject = new math();
Related
In Objects in Kotlin: Create safe singletons in one line of code (KAD 27) Antonio Leiva states:
In fact, an object is just a data type with a single implementation.
I would expect to see the term instance rather than implementation used here. Is there some nuance that I am missing?
Sure it does have a single instance after all, but I believe what they meant to say is that whatever you write in an object is final and you can not override it. Even if you make it open(for argument purpose), you can not make an anonymous object out of it since the anonymous class can't be used on a SingleTon instance.
So " data type with a single implementation" means, whatever you write is the final implementation. An instance is, after all, a result of some implementation.
For reference, I am adding a decompiled code of object declaration.
public final class Test {
#NotNull
private static final String testMember = "Test";
public static final Test INSTANCE;
#NotNull
public final String getTestMember() {
return testMember;
}
private Test() {
}
static {
Test var0 = new Test();
INSTANCE = var0;
testMember = "Test";
}
}
I just started using JProfiler (version 11.0.1). After profiling my test application (instrumentation), I went to "CPU views->Call Graph" and generate the call graph after selecting the classes I am interested in. I have two classes, class A and class B. I see the time taken by each and the invocation count as well. All seems correct so far. In my example, I am passing an integer variable and a double variable from class A to class B. However, I don't know where to look for if I want to see how much data (say in terms of KB) is being sent from class A to class B. Here are the codes I have used:
Class A
public class ClassA {
public static void main(String args[]){
ClassB clsB = new ClassB();
clsB.MethodB1(78);
clsB.MethodB2(999999);
}
}
Class B
public class ClassB {
public void MethodB1(int i){
System.out.println("The value of i is " + i);
}
public void MethodB2(double i){
System.out.println("The value of i is " + i);
}
}
The result is as shown in the image below:
So, my question is where to look for the size of data being sent from ClassA to ClassB?
Thanks in advance.
Measuring the size of objects passed between methods would be prohibitively expensive.
What you can do instead is use allocation recording in the memory views. Then you can see how may objects have been allocated at particular call stacks. With this information you may be able to answer your question.
I would like to create a calculator application that can switch between different number bases. As far as entering digits is concerned, I was thinking the following would be a flexible api:
public interface ICalculator
{
string Enter(INumberElement element);
}
public class BaseTenCalculator : ICalculator
{
public string Enter(INumberElement element)
{
...
}
}
public class BaseTwoCalculator : ICalculator
{
public string Enter(INumberElement element)
{
...
}
}
My problem is that for the BaseTenCalculator, I would like a method
Enter(BaseTenNumberElement element)
and for a BaseTwoCalculator, I would like a method
Enter(BaseTwoNumberElement element)
to make sure only valid digits for that number base get entered. However, the only way I can think of enforcing this constraint is downcasting the 'element' argument in the two different implementations, and throwing an exception if INumberElement is not of the correct type. I feel like this is 'wrong', and I'm missing something. Is there another way? Is it even possible to create a common interface for two different number base calculators?
public interface ICalculator<in T> where T : INumberElement
{
string Enter(T element);
}
public class BaseTenCalculator : ICalculator<BaseTenNumberElement>
{
public string Enter(BaseTenNumberElement element)
{
throw new NotImplementedException();
}
}
public class BaseTwoCalculator : ICalculator<BaseTwoNumberElement>
{
public string Enter(BaseTwoNumberElement element)
{
throw new NotImplementedException();
}
}
I think you're thinking of the problem incorrectly. A number is a number regardless of base. Base is only a visible representation of the number. A good example to work from might be BigInteger. It has a constructor: BigInteger(String val, int radix), and a function: toString(int radix). All the work of representing the number is done the same. The only thing that differs is parsing from a string representation into the number, and then getting back out into a number format in a particular base.
You could create a multi-base calculator by using BigInteger or BigDecimal underneath and just using a base selection to set the radix value to parse or print the number(s). You'd also want to limit the input buttons (assuming you're using buttons), but that's really just a counting problem.
This is the code snippet
public class Square{
private double length;
private double width;
public Square(double a, double b){
length = a;
width = b;
}
public static void main(String args[]){
Square box = new box(3.0,5.0);
}
}
Here is my question, what is the difference between Square box = new box(3.0,5.0); and Square box = new Square(3.0,5.0);
the new box(3.0,5.0) suppose is to be call the box constructor which I don't have. but it will call Square constructor as well. It really confuse me why written in this way also is correct
The difference is new Square(3.0, 5.0) is valid but new box(3.0, 5.0) is a compile error. If it's actually compiling for you then your project must also include another class called box (perhaps in another file) which you haven't included in your post.
If I had a class with immutable members in Java, I would do this:
class MyClass {
private final String name;
private final int id;
myClass(String name, int id) {
this.name = name;
this.id = id;
}
String getName() { return name; }
int getId() { return id; }
}
In Progress-4GL, you'd typically see something like this: (Please, no lectures on Hungarian Notation. I hate it too, but it's very common in the Progress community, so it's something I just live with.)
CLASS MyClass :
DEFINE VARIABLE mcName as CHARACTER NO-UNDO.
DEFINE VARIABLE miId as INTEGER NO-UNDO.
CONSTRUCTOR PUBLIC MyClass(INPUT ipcName AS CHARACTER,
INPUT ipiId AS INTEGER):
ASSIGN mcName = ipcName
miId = ipiID.
END. /* constructor(char,int)*/
END CLASS. /* MyClass */
I was told in that in Progress 10.2B, they added the ability to make constants/final variables. However, I am unable to find any reference to it anywhere. In my Architect (version 10.2A) I do see that FINAL is considered a keyword. But the documentation behind it simply eludes me.
And if you've ever tried to search for Progress documentation, you know my dilemma.
How can I do immutable variables in Progress 10.2B? Are there any gotchyas I need to be aware of?
Thanks!
EDIT 1 I found documentation on FINAL. It appears to only apply to classes and methods. My current approach is
CLASS ImmutableString :
DEFINE PRIVATE VARIABLE mcValue AS CHARACTER NO-UNDO.
CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
ASSIGN mcValue = ipcValue.
END.
METHOD PUBLIC CHARACTER getValue() :
RETURN mcValue. /* Is a defensive copy required? */
END METHOD.
END CLASS.
You could also create a public property with a public "GET" and a private "SET":
DEF PUBLIC PROPERTY Value AS CHAR NO-UNDO
GET.
PRIVATE SET.
CONSTRUCTOR PUBLIC ImmutableString(INPUT ipcValue AS CHARACTER) :
Value = ipcValue.
END.
That's a little less code and does the same thing.
EDITED to change the property name to match the original poster's example.