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.
Related
I am trying to learn Java; here is the exercise I am struggling with:
Fermat’s Last Theorem says that there are no integers a, b, and c such that a^n + b^n = c^n except in the case when n = 2.
Write a method named checkFermat that takes four integers as parameters— a, b, c and n—and that checks to see if Fermat’s theorem holds. If n is greater than 2 and it turns out to be true that a^n + b^n = c^n, the program should print “Holy smokes, Fermat was wrong!” Otherwise the program should print “No, that doesn’t work.”
You should assume that there is a method named raiseToPow that takes two integers as arguments and that raises the first argument to the power of the second. For example:
int x = raiseToPow(2, 3);
would assign the value 8 to x, because 2^3 = 8.
I have encountered several problems, for example I can't seem to use Math.Pow(a, n) with an int, only with a double. If you are interested, here is what I have so far, feel free to skip it and just write your own version of the program in the answers.
(Please keep in mind I started this book only a few days back.)
package fermat.s_last_theorem;
import java.lang.Math;
import java.util.Scanner;
public class FermatS_Last_Theorem {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
System.out.println("Inster First Number");
double frst = s.nextDouble();
System.out.println("Insert Second Number");
double scnd = s.nextDouble();
System.out.println("Insert Exponent");
double expo = s.nextDouble();
double v = FLaw(frst,scnd,expo);
double k = FLawRes(v, expo);
System.out.println("The answer is " + v);
System.out.println("Your answer rooted by your exponent is " + k);
Law(v, Pow(k, expo));
}
public static double Pow(double a, double b) {
double res = Math.pow (a, b);
return (res);
}
public static double FLaw(double frst, double scnd, double expo) {
double D1 = Pow(frst, expo);
double D2 = Pow(scnd, expo);
return (D1 + D2);
}
public static double FLawRes(double res, double base) {
double D3 = Pow(res, 1/base);
return D3;
}
public static void Law(double v, double k) {
if (v==k) {
System.out.println("Pythagora works.");
} else {
System.out.println("Pythagora doesnt work");
}
}
}
The main problem is that I am not exactly sure how to answer the question the exercise asks, and the program listed above does not work as it should.
You should assume that there is a method named raiseToPow ...
That means you write your code using such a method, even though you don't have the method. Your code will be reviewed manually, or teacher may supply the method and run your code.
If you want to test your code, you can always implement it yourself. You should just remove the method before turning in the code.
But the intent here is that this is a write-on-paper exercise.
Now, how to implement int raiseToPow(int a, int b)?
Think about what it means. 34 means 3 * 3 * 3 * 3.
So, implement the method to multiply by a by itself b times.
I'll leave that as another exercise for you.
You can break it out like this :
public boolean checkFermat(int a, int b, int c, int n) {
if(n != 2 &&
(checkFermatCondition(a,b,c,n) ||
checkFermatCondition(a,c,b,n) ||
checkFermatCondition(b,c,a,n))) {
System.out.println("Holy smokes, Fermat was wrong!");
} else {
System.out.println("No, that doesn’t work.");
}
}
In this method you are just trying to reduce you check condition with all of the combinations by calling this method with different parameters
private boolean checkFermatCondition(int a, int b, int c, int n) {
return raiseToPow(a,n)+raiseToPow(b,n) == raiseToPow(c,n);
}
Your function raiseToPow()'s functionality can be achieved using Math.pow:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println( "Fermat's Last Theorem: a^n+b^n != c^n (n!=2)");
int a, b, c, n;
System.out.print("Enter value for a:");
a = s.nextInt();
System.out.print("Enter value for b:");
b = s.nextInt();
System.out.print("Enter value for c:");
c = s.nextInt();
while(true){
System.out.print("Enter value for n:");
n = s.nextInt();
if(n!=2)
break;
System.out.println("n cannot be 2");
}
checkFremat(a,b,c,n);
}
public static void checkFremat(int a, int b, int c, int n){
if ((int)Math.pow(a, n)+(int)Math.pow(b, n)!=(int)Math.pow(c, n))
System.out.println("Fermat was correct!");
else
System.out.println("Holy smokes, Fermat was wrong!");
}
}
Try it here!
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
import java.util.Scanner;
public class Recursion
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n to start: ");
int n = in.nextInt();
System.out.println("Sum of numbers from 1 to " + n + ": " +new Recursion().sumUpTo(n));
}
}
/**
* Computes the sum of a range of numbers
*
* #param n an integer
* #return the sum of n range
*/
public int sumUpTo(int num){
if (num == 0)
{
return 0;
}
else{
return (num + sumUpTo(num-1))
}
}
This should be a very simple method, I know, but I can't seem to get it to compile. I keep getting "class, interface, or enum expected" on the public int sumUpTo(int num) line. This is the method that performs the actual computations. Any help would be greatly appreciated.
A couple things :
Your method is declared outside of the class
Missing semicolon on the line with your recursive call
You should be new to Object Oriented Programming. In Java a function(method) must be inside(belong) to a class. You either put public int sumUpTo(int num) inside public class Recursion or you can create another class and put it in there. But remember there should be only one public class inside a file.
In Java, everything must be inside of a class. Simply move your method to within the curly braces of the class and it should work.
The error class, interface, or enum expected is quite self-explained! It encountered code that wasn't inside of a class, inteface, or enum, and it wasn't expecting that, because that shouldn't happen!
In case you need to see what I mean:
public class Recursion {
public static void main (String[] args) {
// your code here...
}
// Where your method should have gone.
}
Also, welcome to Stack Overflow. Please remember to accept an answer if it answers your question.
Move your sumUpTo method inside the class.
import java.util.Scanner;
public class Recursion
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter n to start: ");
int n = in.nextInt();
System.out.println("Sum of numbers from 1 to " + n + ": " +new Recursion().sumUpTo(n));
}
/**
* Computes the sum of a range of numbers
*
* #param n an integer
* #return the sum of n range
*/
public int sumUpTo(int num){
if (num == 0)
{
return 0;
}
else{
return (num + sumUpTo(num-1));
}
}
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Say I want to combine two programs into one so that when I run the combined program, both of the outputs from the individual programs are printed. How do I write the code to accomplish this?
Program 1:
public class Number1 {
public static void main (String[] args){
double s0=1.0;
double v0=2.0;
double a=9.8;
double t=3.0;
double s;
System.out.println(s0+v0*t+0.5*a*t*t);
}
}
Program 2:
public class Number2 {
public static void main (String[] args) {
for (int i=1; i<=10; i++){
System.out.print(i*i + " ");
}
System.out.println("");
}
}
A possible solution would be to call the static methods of both classes main
public class Number3 {
public static void main (String[] args) {
Number1.main(args);
Number2.main(args);
}
}
This assumes the Number1 and Number2 are within the classpath of Number3 of course...
public class Number1 {
public static void main (String[] args){
double s0=1.0;
double v0=2.0;
double a=9.8;
double t=3.0;
double s;
System.out.println(s0+v0*t+0.5*a*t*t);
Number2.main(args);
}
}
If both the classes are in the same package then just call one main method of one of the class inside another class.Here I called main method of second class in first class
Simply paste the code of your Number2 class main() method code in Number1's class main() method.
You can write the whole code like this:
public class Combine {
public void getFirstOne() {
double s0 = 1.0;
double v0 = 2.0;
double a = 9.8;
double t = 3.0;
double s;
System.out.println(s0 + v0 * t + 0.5 * a * t * t);
}
public void getSecondOne() {
for (int i = 1; i <= 10; i++) {
System.out.print(i * i + " ");
}
System.out.println("");
}
public static void main(String[] args) {
Combine combine = new Combine();
combine.getFirstOne();
combine.getSecondOne();
}
}