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();
}
}
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 4 months ago.
Improve this question
I had to rename two files, and now they won't work when I run the demo file. I am a beginner to programming and cannot identify why my methods will not properly connect to the other file containing its constructors, methods, etc. Can someone please help me identify what is going wrong? Specifically, myCube.calcCubeArea(side) is not working, and I don't know why.
import java.util.Scanner;
public class keelanbaxter_185A01_Cubedemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the type of units: ");
String units = sc.next();
System.out.print("Enter the number of sides: ");
double side = sc.nextInt();
keelanbaxter_185A01_Cubedemo myCube1 = new keelanbaxter_185A01_Cubedemo();
myCube1.setSide(side);
System.out.println("The units you have chosen are: " + units + " and the side length is: " + side + units);
System.out.println("The area of the cube is: " + myCube1.calcCubeArea(side));
System.out.println("The volume of the cube is: " + myCube1.calcCubeVolume(side));
}
}
public class keelanbaxter_185A01_Cube {
private double side;
private static String units;
public void setSide(double S) {
side = S;
}
public double getSide() {
return side;
}
public void setUnits(String uni) {
units = uni;
}
public String getUnits() {
return units;
}
public double calcCubeVolume(double volume) {
return volume = side*side*side;
}
public double calcCubeArea(double area) {
return area = side*side;
}
}
The error is that you are not calling the correct class name, change this part:
keelanbaxter_185A01_Cube myCube1 = new keelanbaxter_185A01_Cube();
myCube1.setSide(side);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Basically I have to write a program that uses data that was inputted by the user and put it in a mathematical formula so it spits out an answer. The formula has to be created as a method. This is what I have so far. When I run the code it lets me input a number, but when I do that the program does not output anything and it just finishes running.
import java.util.Scanner;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
String radius = keyboard.nextLine();
}
private static void myMethod() {
}
You need to call the method passing the radius as parameter of the method. You can check https://www.tutorialspoint.com/java/java_methods.htm
,this link to see how to create and call methods with parameters.
I think what you wanted to do is the following:
import java.util.*;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
int area = myMethod(radius);
System.out.println(area);
}
}
Below is what you were trying to do I think
public class Formula {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
float output = myMethod(radius);
System.out.println(output);
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
Write a Java program with a recursive method that accepts a value i as input and computes the sum:
m(i) = 1/3 + 2/5 + 3/7 + 4/9 + 5/11 + ... + i/(2i + 1)
So far I have:
import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
if (i == 0) {
System.out.println(0);
}
else {
System.out.println(i / (2 * i + 1) + (main(i-1)));
}
}
}
The code isn't compiling. Please help!!
Ok I think I finally understand what you're trying to do! The problem is you're actually trying to build a string! The method is doing what it should, your return just isn't really doing what it should.
import java.util.Scanner;
public class Recursion
{
private static String result = null;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
System.out.println(recursion(i));
}
public static String recursion(int i)
{
if(i != 0)
{
if(result == null)
{
result = (i + "/" + (2 * i + 1));
recursion(i - 1);
}
else
{
result = result.concat(" + ").concat(i + "/" + (2 * i + 1));
recursion(i - 1);
}
}
return result;
}
}
Please let me know if this does what you want it to!
You shouldn't use recursion on the main function. Just create a second function that isn't the main and it should work:
import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
System.out.println(recursion(i));
}
public int recursion(int i){
if (i == 0) {
return 0;
}
else {
return (i / (2 * i + 1) + (recursion(i-1)));
}
}
}
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
This is a working program, however I do not know what "return answer' accomplished here, only that something needs to be returned. Isn't there a way to use "answer" in the next method so number1-4 do not have to be retyped.
public class AverageOfGivenNumbers {
// implement
public static int sum(int number1, int number2, int number3, int number4) {
int answer = number1 + number2 + number3 +number4;
return answer;
}
public static double average(int number1, int number2, int number3, int number4) {
double average = sum(number1,number2,number3,number4) / 4.0;
return average;
}
public static void main(String[] args) {
double result = average(4, 3, 6, 1);
System.out.println("Average: " + result);
}
}
This looks like homework, so it is likely just to demonstrate passing parameters.
As I said in my comments, you could shorten everything to this:
public class AverageOfGivenNumbers {
public static int sum(int n1,int n2,int n3,int n4) {
return n1+n2+n3+n4;
}
public static double average(int n1,int n2,int n3,int n4) {
return sum(n1,n2,n3,n4)/4.0;
}
public static void main(String[] args) {
System.out.println("Average: "+average(4,3,6,1));
}
}
When we use return, we are saying what a function will evaluate to when we execute it.
This is shown in the main method where we use average(4,3,6,1).
It becomes (or, "returns") 3.5 and that's what we see when we print it.
Average: 3.5
It doesn't show Average: average(4,3,6,1) because average(4,3,6,1) evaluates to 3.5 - the value we return.
Likewise, when we say return sum(4,3,6,1)/4.0 - that becomes return 14/4.0.
You could also use this in java to pass unlimited arguments:
public static int sum(int... args) {
int sum = 0;
for (int arg : args)
sum += arg;
return sum;
}
public static double average(int... args) {
return (sum(args)/(float)args.length);
}
public static void main(String []args){
System.out.println("Average: "+average(4,3,6,1));
}
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));
}
}
}