Trying to print out a^n but i get the error that a and n aren't defined as variables. Here is what I have now.
public class FermatsTheorem {
public static void main(String[] args) {
fermatsTheorem(a, n);
}
public static void fermatsTheorem(double a, double n){
double aToTheNthPower = Math.pow(a,n);
System.out.println("Fermat's Last Theorem: " + aToTheNthPower);
}
}
You need to declare values for a and n.
Imagine you had an equation x + y = result. How could you know what the value of result is if you don't know the values of x and y?
Same thing for your case. You are trying to compute what a^n equals but you don't give the program any values to compute.
public class FermatsTheorem {
public static void main(String[] args) {
double a = 3.5;
double n = 2.0;
fermatsTheorem(a, n);
}
public static void fermatsTheorem(double a, double n){
double aToTheNthPower = Math.pow(a,n);
System.out.println("Fermat's Last Theorem: " + aToTheNthPower);
}
}
You're looking for a Symbolic Manipulation. This is not a feature of most programming languages. You can't actually deal with abstracts, only concrete values. There are quite possibly libraries available to do what you want in your language of choice, but you may also want to look at something like Mathlab.
Related
So I'm creating a method in that is supposed to print the numbers between two specified numbers. I had it setup before in the main method but i can figure out how to make a method and call it into the main method. What my program does right now is it only prints what "int between" is equal to. I don't want anyone to just type the code out for me, i'm just looking for tips on what i should change. Last time i asked a question someone proceeded to just answer with code and it did not help me to learn anything.
So my question is what is causing the program to only display what between is equal to? I know that I need a value for the if loop to return something, but it needs to return the numbers between num1 & num2. My Professor Also said that the method needs to be "public static void Printer(int num1, int num2)" is that even possible? I kept getting an error so I switched to "int Printer".
package nortonaw_Assignment8;
public class Nortonaw_Assignment8 {
public static void main(String[] args) {
int between;
between = Printer(5, 20);
System.out.println(between);
}
public static int Printer (int num1, int num2){
int between = 0;
for (;num1<=num2; num1++);
return between;
}
}
1) There's a difference between printing out a value and returning a value.
When you"print" a value in a function, you are just writing out the value to screen or some other medium , but when you use the return statement, you are passing what you are returning to the caller of your function as well as control.
** I hope that makes sense to you?
2)"public static void Printer(int num1, int num2)" is possible.
currently your method is designed to return only one single number, so either you return a collection of numbers are you print the numbers inside Printer, in this case you can use the method signature suggested by your professor.
So I would write it like this:
public static void main(String[] args) {
Printer(5, 20);
}
public static void Printer (int num1, int num2) {
for (int i=num1;i<=num2; i++) {
System.out.println(i);
}
}
EDIT:
Note that I introduced a additional counter variable i because I think num1 and num2 should not be changed as they define the boundary of your range.
package printingTasks;
public class Printer {
public static void main(String[] args) {
printInBetween(25, 30); //Would print 26 -> 29
}
/*
* You don't need to return something if you just want to print it out to the console.
* So you can use void as return type
*/
public static void printInBetween(final int leftBoundary, final int rightBoundary){
/**
* Calculate the first number which should be printed.
* This number would be the leftBoundery plus one
* This number will be the starting point of your loop
*/
final int firstNumber = leftBoundary + 1;
// final int firstNumber = leftBoundary; //if you want to include the left boundary
for (
int currentNumber = firstNumber; //Set the counter of the the loop (currentNumber) to the first valid number
currentNumber < rightBoundary; //Run the loop while the loop counter is less than the rightBoundary
// currentNumber <= rightBoundary; //If you want to include the right boundary
currentNumber++ //Increment the loop counter with each iteration
){
/**
* In each iteration you will print the current value of the counter to the console
* Because your counter (currentNumber) will be incremented from the first valid number
* to the last number before the right boundary you will get all numbers between the two
* boundaries.
*/
System.out.println(currentNumber);
}
}
}
Your teacher wants you to print from the Printer method not directly in the main method. All the main method should be doing is calling the Printer method.
Here is the full snippit:
package nortonaw_Assignment8;
public class Nortonaw_Assignment8 {
public static void main(String[] args) {
Printer(5, 20);
}
public static void Printer (int num1, int num2) {
for (int i = num1+1;i<=num2-1; i++) {
System.out.println(i);
}
}
}
I'm in a beginning programming class, and a lot of this had made sense to me up until this point, where we've started working with methods and I'm not entirely sure I understand the "static," "void," and "return" statements.
For this assignment in particular, I thought I had it all figured out, but it says it "can not find symbol histogram" on the line in the main method, although I'm clearly returning it from another method. Anyone able to help me out?
Assignment: "You see that you may need histograms often in writing your programs so you decide for this program to use your program 310a2 Histograms. You may add to this program or use it as a class. You will also write a class (or method) that will generate random number in various ranges. You may want to have the asterisks represent different values (1, 100, or 1000 units). You may also wish to use a character other than the asterisk such as the $ to represent the units of your graph. Run the program sufficient number of times to illustrate the programs various abilities.
Statements Required: output, loop control, decision making, class (optional), methods.
Sample Output:
Sales for October
Day Daily Sales Graph
2 37081 *************************************
3 28355 ****************************
4 39158 ***************************************
5 24904 ************************
6 28879 ****************************
7 13348 *************
"
Here's what I have:
import java.util.Random;
public class prog310t
{
public static int randInt(int randomNum) //determines the random value for the day
{
Random rand = new Random();
randomNum = rand.nextInt((40000 - 1000) + 1) + 10000;
return randomNum;
}
public String histogram (int randomNum) //creates the histogram string
{
String histogram = "";
int roundedRandom = (randomNum/1000);
int ceiling = roundedRandom;
for (int k = 1; k < ceiling; k++)
{
histogram = histogram + "*";
}
return histogram;
}
public void main(String[] Args)
{
System.out.println("Sales for October\n");
System.out.println("Day Daily Sales Graph");
for (int k = 2; k < 31; k++)
{
if (k == 8 || k == 15 || k == 22 || k == 29)
{
k++;
}
System.out.print(k + " ");
int randomNum = 0;
randInt(randomNum);
System.out.print(randomNum + " ");
histogram (randomNum);
System.out.print(histogram + "\n");
}
}
}
Edit: Thanks to you guys, now I've figured out what static means. Now I have a new problem; the program runs, but histogram is returning as empty. Can someone help me understand why? New Code:
import java.util.Random;
public class prog310t
{
public static int randInt(int randomNum) //determines the random value for the day
{
Random rand = new Random();
randomNum = rand.nextInt((40000 - 1000) + 1) + 10000;
return randomNum;
}
public static String histogram (int marketValue) //creates the histogram string
{
String histogram = "";
int roundedRandom = (marketValue/1000);
int ceiling = roundedRandom;
for (int k = 1; k < ceiling; k++)
{
histogram = histogram + "*";
}
return histogram;
}
public static void main(String[] Args)
{
System.out.println("Sales for October\n");
System.out.println("Day Daily Sales Graph");
for (int k = 2; k < 31; k++)
{
if (k == 8 || k == 15 || k == 22 || k == 29)
{
k++;
}
System.out.print(k + " ");
int randomNum = 0;
int marketValue = randInt(randomNum);
System.out.print(marketValue + " ");
String newHistogram = histogram (randomNum);
System.out.print(newHistogram + "\n");
}
}
}
You're correct that your issues are rooted in not understanding static. There are many resources on this, but suffice to say here that something static belongs to a Class whereas something that isn't static belogns to a specific instance. That means that
public class A{
public static int b;
public int x;
public int doStuff(){
return x;
}
public static void main(String[] args){
System.out.println(b); //Valid. Who's b? A (the class we are in)'s b.
System.out.println(x); //Error. Who's x? no instance provided, so we don't know.
doStuff(); //Error. Who are we calling doStuff() on? Which instance?
A a = new A();
System.out.println(a.x); //Valid. Who's x? a (an instance of A)'s x.
}
}
So related to that your method histogram isn't static, so you need an instance to call it. You shouldn't need an instance though; just make the method static:
Change public String histogram(int randomNum) to public static String histogram(int randomNum).
With that done, the line histogram(randomNum); becomes valid. However, you'll still get an error on System.out.print(histogram + "\n");, because histogram as defined here is a function, not a variable. This is related to the return statement. When something says return x (for any value of x), it is saying to terminate the current method call and yield the value x to whoever called the method.
For example, consider the expression 2 + 3. If you were to say int x = 2 + 3, you would expect x to have value 5 afterwards. Now consider a method:
public static int plus(int a, int b){
return a + b;
}
And the statement: int x = plus(2, 3);. Same here, we would expect x to have value 5 afterwards. The computation is done, and whoever is waiting on that value (of type int) receives and uses the value however a single value of that type would be used in place of it. For example:
int x = plus(plus(1,2),plus(3,plus(4,1)); -> x has value 11.
Back to your example: you need to assign a variable to the String value returned from histogram(randomNum);, as such:
Change histogram(randomNum) to String s = histogram(randomNum).
This will make it all compile, but you'll hit one final roadblock: The thing won't run! This is because a runnable main method needs to be static. So change your main method to have the signature:
public static void main(String[] args){...}
Then hit the green button!
For starters your main method should be static:
public static void main(String[] Args)
Instance methods can not be called without an instance of the class they belong to where static methods can be called without an instance. So if you want to call your other methods inside the main method they must also be static unless you create an object of type prog310t then use the object to call the methods example:
public static void main(String[] Args)
{
prog310t test = new prog310t();
test.histogram(1);
}
But in your case you probably want to do:
public static String histogram (int randomNum)
public static void main(String[] Args)
{
histogram(1);
}
Also you are not catching the return of histogram() method in your main method you should do like this:
System.out.print(histogram(randomNum) + "\n");
Or
String test = histogram(randomNum);
System.out.print(test + "\n");
Static methods are part of a class and can be called without an instance but instance methods can only be called from an instance example:
public class Test
{
public static void main(String[] args)
{
getNothingStatic();// this is ok
getNothing(); // THIS IS NOT OK IT WON'T WORK NEEDS AN INSTANCE
Test test = new Test();
test.getNothing(); // this is ok
getString(); // this is ok but you are not capturing the return value
String myString = getString(); // now the return string is stored in myString for later use
}
public void getNothing()
{
}
public static void getNothingStatic()
{
}
public static String getString()
{
return "hello";
}
}
Void means the method is not returning anything it is just doing some processing. You can return primitive or Object types in place of void but in your method you must specify a return if you don't use void.
Before calling histogrom (randomNum) you need to either make histogram static or declare the object that has histogram as a method
e.g
prog310t myClass = new prog310t();
myClass.histogram()
public class Triangle {
public static void main(String[] args) {
int n=12345, x=15;
int res =(n % x);
System.out.println(res);
}
}
The statement int res = (n%x) is 0. Why?
When you do,
12345/15
It exactly divides it by 823 times and reminder is zero.
There is nothing wrong it with %. Make sure that you want reminder or the result of n/x
Create a program which simulates a very simple calculator
So I have been asked to implement an abstract class that represents binary (having 2 arguments) arithmetic expression
abstract class ArithmeticExpression {
double binary1;
double binary2;
public abstract void evaluate ();
public abstract void display ();
}
so then I created sub classes add, multiply, subtract, and divide. In subtract I have:
public class subtract extends ArithmeticExpression {
// private double result;
double binary1;
double binary2;
public subtract (double x, double y) {
this.binary1 = x;
this.binary2 = y;
}
public void evaluate () {
System.out.println("Expression: " + getsubX() + " - " + getsubY() + " = ");
// return result;
}
public void display () {
}
public double getsubX() {
}
public double getsubY() {
}
Using the classes I should be able to represent any arbitrary expression, with no hard coding.
It is also said evaluate should return the result as double and display method should print the expression out in string. Am I on the right track? What am I missing here? The part I do not understand how it is able to represent any expression?
Using your abstract ArithmeticExpression, here's what the Subtract class should look like. Java classes start with a capital letter.
public class Subtract extends ArithmeticExpression {
double result;
public Subtract(double x, double y) {
this.binary1 = x;
this.binary2 = y;
}
#Override
public void evaluate() {
result = binary1 - binary2;
}
#Override
public void display() {
System.out.println("Expression: " + binary1 +
" - " + binary2 + " = " + result);
}
}
You don't have to re-declare binary1 and binary2. They are instantiated in the abstract ArithmeticExpression class.
You do have to provide a double for the result. This should have been done in the abstract ArithmeticExpression class.
The evaluate() method is for evaluation.
The display() method is for display.
You don't have to define any other methods in your Subtract concrete class.
If you want to evaluate any expession inserted in form of
4 + 3 * ( 4 + 5)
You either need to create binary tree or stack and fill those values and operators in.
What I quite dont understand is your so called binary represented in double. If you want to have binary calc, you should use unsigned int or long (or any other type, that is not floating point)
Do you see anything wrong in this code? in thosen't work well it returns a NaN.
public class Method2 extends GUIct1
{
double x=0,y=0;
void settype1 (double conv1)
{
x = conv1;
}
void settype2 (double conv2)
{
y = conv2;
}
double conversion ( double amount)
{
double converted = (amount*y)/x;
return converted;
}
}
Way it is used an i already changed the set part
Method2 convert = new Method2(); \\ method is called
.....
convert.settype1(j);
.....
convert.settype2(k);
.....
double x = convert.conversion(i);
System.out.println(x);
Well, the fact that you've got methods which set variables called get-something is pretty obviously not a good idea, and there's no indentation... but it should work. But then, you haven't shown how you're using it. Perhaps you're not actually called the setter methods?
Here's an example of the same code but with different names, and a sample of using it:
class Converter
{
double multiplier = 0;
double divisor = 0;
void setMultiplier(double multiplier)
{
this.multiplier = multiplier;
}
void setDivisor(double divisor)
{
this.divisor = divisor;
}
double convert(double amount)
{
return (amount * multiplier) / divisor;
}
}
public class Test
{
public static void main(String[] args)
{
Converter converter = new Converter();
converter.setMultiplier(3.5);
converter.setDivisor(8.5);
System.out.println(converter.convert(2)); // Prints 0.8235294117647058
}
}
Personally I'd probably make the variables final and set them in the constructor, but that's another matter...
It doesn't look like you ever call gettype1 or gettype2 so the x/y is 0/0 resulting in NaN
This can happen when x=0. Division by zero is not defined. Print out x and y before you start to calculate to see whetther they are not zero.