Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please Help me in my code. This is polymorphism! I can't call all my class as you can see below, only the addition shows the output. I need some explanation too because I really need to know how this happened.
This is my code:
import java.util.Scanner;
class Variation{
public void reality(){
}
public int reality(int n1,int n2,int n3,int n4,int n5){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 5 numbers for addition: ");
n1=keyboard.nextInt();
n2=keyboard.nextInt();
n3=keyboard.nextInt();
n4=keyboard.nextInt();
n5=keyboard.nextInt();
int sum=n1+n2+n3+n4+n5;
System.out.println("The answer is: "+sum);
return sum;
}
}
class multi extends Variation{
public double reality(double n1,double n2, double n3, double n4, double n5){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 5 numbers for multiplication: ");
n1=keyboard.nextInt();
n2=keyboard.nextInt();
n3=keyboard.nextInt();
n4=keyboard.nextInt();
n5=keyboard.nextInt();
double prod=n1*n2*n3*n4*n5;
System.out.println("The answer is: "+prod);
return prod;
}
}
class sub extends Variation{
public int reality(int n1,int n2, int n3, int n4, int n5){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 5 numbers for Subtraction: ");
n1=keyboard.nextInt();
n2=keyboard.nextInt();
n3=keyboard.nextInt();
n4=keyboard.nextInt();
n5=keyboard.nextInt();
int diff=n1-n2-n3-n4-n5;
System.out.println("The answer is: "+diff);
return diff;
}
}
class Calling
{
public static void main(String[] args) {
int n1=0,n2=0,n3=0,n4=0,n5=0;
Variation variaTions = new Variation();
System.out.println(variaTions.reality(n1, n2, n3, n4, n5));
}
}
You only ever create an instance of Variation. You never create an instance of any of the derived classes. So of course they won't be called.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
alright so i have already made a working compound interest rate program. but now for this program i have to re write the program using more methods. using
private static double FVCALC(..)
and
private static double validate(........)
i dont quite understand how i need to do this. the current code i have only lets me input the 3 values of interest rate and it stops. is it because of the private mehtods? im not sure what to do and i have searched for 3 days now.
bottom line is. my code is not working the way i want it to.
public class interest_rate
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double i;
double n;
double FVCalc;
double PV;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
System.out.print("please enter Present Value: ");
PV = input.nextDouble();
}
private static double validate (double upLimit, double lowLimit, double PV)
{
upLimit=100000.00;
lowLimit=0.00;
while(PV>upLimit|| PV<lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.print("please enter PV");
PV=input.nextDouble();
}
return PV;
}
private static double FVCalc(double PV, double i, double n, double FV)
{
FV = PV*Math.pow(1+(i/100), n);
return(FV);
}
}
First, you need to call the methods in main.
Secondly, you cant pass in PV, then reassign it and expect the value to update.
For example..
private static double validate (double upLimit, double lowLimit, double PV)
You need to call this in main like so
PV = 0.0; // some double value
double validated = validate(0,100000); // return the value, don't pass it in
And remove these lines in that method because overriding parameters is typically bad.
upLimit=100000.00;
lowLimit=0.00;
Next, add a field for your values that you want to use throughout the class.
public class InterestRate
{
static double pv , fvCalc;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
}
And remove these lines in main and use those two class variables instead
double FVCalc;
double PV;
Additionally, I don't see a reason to store fvCalc as a variable. You can just calculate it
private static double fvCalc(double pV, double i, double n)
{
return pV*Math.pow(1+(i/100), n);
}
i believe i have figured it out. and i made it pretty. excuse the notes. i wanted to keep them there to see what i did wrong. thanks all.
public class interest_rate
{
static double pV , fvCalc, i, n;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double validated;
double calculated;
double i;
double n;
//double fV;
//double pV1;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
validated = validate(0,100000);
System.out.println("pV is validated and equal to: "+validated);
calculated= fvCalc(validated,i,n);
System.out.println("your calulation for interest is: "+calculated);
}
private static double validate(double upLimit, double lowLimit) {
double pV;
System.out.print("please enter pV");
pV=input.nextDouble();
while(pV<upLimit|| pV>lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.println("please enter pV");
pV=input.nextDouble();
}
return pV;
}
private static double fvCalc(double pV, double i, double n)
{
double fV;
fV=pV*Math.pow(1+(i/100), n);
return fV;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am still a bit new to Java and I could use some help with this code please, so far i wrote the methods and what each methods should do but I honestly have no idea how to do the overloading effect and make it work so I would appreciate a simple explanation.
import java.util.Scanner;
public class Assignment3 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
myMethod();
}
public static void myMethod(){
System.out.println("Welcome to Java 1 ");
}
public static void myMethod(String msg, int counter){
System.out.println("Enter your custom messege please: ");
msg = input.nextLine();
System.out.println("Please enter how many times do you wish to print the messsege: ");
counter = input.nextInt();
for (int i = 0; i <= counter; i++){
System.out.println(msg);
}
}
public static void myMethod(int lowerLimit, int upperLimit){
System.out.println("Please enter a lowerlimit: ");
lowerLimit = input.nextInt();
System.out.println("Please enter an upperlimit: ");
upperLimit = input.nextInt();
System.out.println("Press 1 for ascending order: ");
System.out.println("Press 2 for descending order: ");
System.out.println("Make your selection");
int user1 = input.nextInt();
System.out.println("How frequent do you wish the messege to be printed");
int interval = input.nextInt();
switch(user1){
case 1:
for(int counter = lowerLimit; counter <= upperLimit; counter += interval){
System.out.println(counter);
}
break;
case 2:
for(int counter = upperLimit; counter <= lowerLimit; counter -= interval){
System.out.println(counter);
}
break;
default :
System.out.println("Something went wrong !!!");
}
}
public static void myMethod(double number1, double number2){
number1 = (Math.random() * 100);
number2 = (Math.random() * 100);
double product = (number1 * number2);
System.out.println("The product of " + number1 + " and " + number2 + " is " + product);
}
]
Your myMethod method is already overloaded. An overloaded method is just a method that can accept two or more different sets of parameters. (see https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html)
For example :
public void foo(int a) {
System.out.println("Printing out the int " + a);
}
public void foo(double a) {
System.out.println("Printing out the double " + a);
}
Foo has two possible parameter sets, one that accepts an int and one that accepts a double. Now, if you do this :
int a = 10;
double b = 10.5;
foo(a);
foo(b);
It'll return :
Printing out the int 10
Printing out the double 10.5
In response to your comment:
You just need to call the two other "myMethod" in your main, with their respective signatures:
public static void main(String[] args) {
// Call without argument
myMethod();
// Call with String and integer
myMethod("test", 42);
// Call with Integer and Integer
myMethod(42, 666);
}
The right ones will be called then. Does this answer your question ?
Above post has your answer, Your myMethod method is already overloaded but Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different.
You have your method which accepts different parameters with different datatypes
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to have the code print out the highest of the three grades; however, the if/else statement in the highest method is finding an error in return d. I have tried putting if(d>b && d>c) return d, and also else return d. But both times the program says it is unreachable. Can someone explain what I'm doing wrong? Thank you!
import java.util.Scanner;
public class Methods2 {
public static double average(double a){
double ave= a/3.0;
return ave;
}
public static double highest(double b, double c, double d){
if(b>c && b>d)
return b;
if(c>b && c>d);
return c;
return d;//unreachable code
}
public static void main(String[] args){
Scanner kb= new Scanner(System.in);
System.out.println("Enter your name.");
String name = kb.nextLine();
System.out.println("Enter your three grades.");
double b= kb.nextDouble();
double c= kb.nextDouble();
double d= kb.nextDouble();
double av= average(b+c+d);
System.out.println(av);
double high= highest(b,c,d);
System.out.println(high);
}
}
Yes. Because semicolon makes it an empty if body.
if(c>b && c>d);
return c;
should be
if(c>b && c>d)
return c;
or (the arguably better)
if(c>b && c>d) {
return c;
}
You could also use Math.max(double, double) to make this a one line method like
public static double highest(double b, double c, double d) {
return Math.max(d, Math.max(b, c));
}
because of the semicolon in the second if
if(c>b && c>d);
That terminates the conditional statement right there and return c; becomes a statement that will get executed regardless of condition, remove it
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to finish my rational class for java and everywhere I have looked to make it finished doesn't have it near the same. I know I could use others programs that where made but the ones I have seen don't have it for where you put the input in when you run the program. This is the code I have so far
import java.util.Scanner;
public class Lab09ast
{
private static int num, den; // numerator and denominator of the rational number
public static void main (String[] args)
{
enterData();
Rational r = new Rational(num,den);
r.displayData();
}
public static void enterData()
{
Scanner input = new Scanner(System.in);
System.out.print("\nEnter the numerator ----> ");
num = input.nextInt();
System.out.print("\nEnter the denominator --> ");
den = input.nextInt();
}
}
class Rational
{
public void displayData()
{
System.out.println();
System.out.println(getNum() + "/" + getDen() + " equals " + getDecimal());
System.out.println();
}
private void getGCF(int n1,int n2)
{
int rem = 0;
do
{
rem = n1 % n2;
if (rem == 0)
gcf = n2;
else
{
n1 = n2;
n2 = rem;
}
}
while (rem != 0);
}
}
Member variables num and den (numerator and denominator) are in class Lab09ast. These should be in class Rational. Do you understand the concepts of classes and objects?
It's logical that a Rational object, which you make from the class Rational, has member variables for the numerator and denominator.
Also, those member variables must not be static. See Understanding Class Members to learn what static means and why it is not appropriate for these member variables.
The methods getNum() and getDen() should return the values of the num and den member variables, and should also be in class Rational.
Class Rational should also have a constructor that takes two arguments, for the numerator and denominator. You're already calling that constructor in the main method of class Lab09ast, but it's not in your class Rational yet.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have managed to do part of this question but am having an issue with the cube method.
I need to call the square method from within the cube method to return the cube result.
Example: To square the number 5 the result will be 25. I then call this method in to the cube method to return the answer 125.
Can someone please tell me where I am going wrong please?
Here is my code:
import java.util.*;
public class ExamPaper2011
{
public static void main(String [] args){
int totalSquared = 0;
int totalCubed = 0;
cubedNumber(totalSquared, totalCubed);
}
public static int squaredNumber(int totalSquared){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a number to square: ");
int numSquare = in.nextInt();
System.out.println("You entered " + numSquare);
totalSquared = (int) Math.pow (numSquare, 2);
System.out.println("The number squared is " + totalSquared);
return totalSquared;
}
public static int cubedNumber(int totalSquared, int totalCubed){
squaredNumber(totalSquared);
totalSquared = (int) Math.sqrt(totalSquared * totalSquared);
System.out.println(totalSquared);
totalCubed = totalSquared;
totalCubed = (int) Math.pow (numSquare, 3);
return totalCubed;
}
}
The method cubedNumber seems to return a 0.
Any help is greatly appreciated. Please forgive my basic code. This is a class session.
Here is the answer. Thank you again all.
import java.util.*;
public class ExamPaper2011
{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a number to square and cube: ");
int n = in.nextInt();
cubedNumber(n);
}
public static int squaredNumber(int n){//Question 4
System.out.println("You entered " + n);
n = n * n;
System.out.println("Squared = " + n);
return n;
}
public static int cubedNumber(int n){
squaredNumber(n);
n = n * squaredNumber(n);
System.out.println("Cubed = " + n);
return n;
}
}
I appreciate this great feedback. Really helps. Thank you all.
how about moving user input checking part out of your logic methods?
public class ExamPaper2011
{
public static void main(String [] args){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a number: ");
//here you get user input, maybe ask user what calculation he wants to do ^2 Or ^3
//...get n from user input.
//if he wants square
print squaredNumber(n);
//if he wants cubed
print cubedNumber(n);
}
public static int squaredNumber(int n){
return n*n;
}
public static int cubedNumber(int n){
return n*squaredNumber(n);
}
}