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 6 years ago.
Improve this question
*Edited. I ended up with the following.
I was given this code.
package lab2;
/**
* Model of a basketball for use in quality control simulations.
*/
public class Basketball
/**
* Inflation status of this Basketball.
*/
private boolean isInflated;
/**
* Diameter of this Basketball.
*/
private double diameter;
/**
* Constructs an uninflated Basketball with the given diameter.
* #param givenDiameter
* the diameter for this Basketball
*/
public Basketball(double givenDiameter)
{
isInflated = false;
diameter = givenDiameter;
}
/**
* Returns the diameter of this Basketball.
* #return
* diameter of this Basketball
*/
public double getDiameter()
{
return diameter;
}
/**
* Determines whether this Basketball can be dribbled.
* #return
* true if the basketball is inflated, false otherwise
*/
public boolean isDribbleable()
{
// can be dribbled only if it is inflated
return isInflated;
}
/**
* Returns the circumference of this Basketball.
* #return
* circumference of this Basketball
*/
public double getCircumference()
{
// PI times the diameter
double result = Math.PI * diameter;
return result;
}
/**
* Inflates this Basketball.
*/
public void inflate()
{
isInflated = true;
}
}
The only change I did was chaning the Basketball class to
public class Basketball extends java.lang.Object
Then I wrote the following code and saved it in the same folder as the the one above.
public class BasketballTest {
public static void main (String[] args){
Basketball b;
b = new Basketball(4.0);
System.out.println(b.getDiameter());
}
}
The first code compiles fine, but when compiling the second I get the following error.
BasketballTest.java:5: error: cannot access Basketball
Basketball b;
^
bad class file: .\Basketball.class
class file contains wrong class: lab2.Basketball
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
I hope someone help understanding where the problems are. I am not in a class, but doing this assignment to learn Java.
The link to the assignment http://www.briannakayama.com/COMS227/Labs/Lab2/
Thanks
First, format your code well. Second, I don't see your link. Lastly, these kind of questions are not for stack overflow but I'll answer it anyway.
Package is basically like a folder that classes and other files are stored. Project is the whole thing - group of packages. You are good with what you have.
You didn't specified what kind of error this is.... BUT I'm guessing you probably didn't make basketball class. Or put correct constructor in basketball class. Or.... I don't know. Tell me what your error it is.
OH now I see it.
You were suppose to put that file in same folder as the basketballTest class. If don't, that will give you an error.(because compt have no idea what basketball is) Or you can make a whole new class and just copy the code.
1) Consider project as a directory (eg: C Drive), package as a folder in the drive and class as .java file.
2). I don't know what do you mean by Basketball b;as Basketball is not a data type and looking at the line below you are trying to assign it float value.
Probably you need something like this,
public class BasketballTest
{
double b;
BasketballTest() //Constructor
{
this.b = 4.0;
}
void display(){System.out.println(b);}
public static void main (String [] args){
BasketballTest obj = new BasketballTest();
obj.display();
}
}
That's because you don't have Basketball class inside your folder. Check your folder positions and your package declarations.
And for the question, I believe that it wants you to make variable 'b' to execute method to change isInflated to true; I'm not sure about your homework link. If you know what you are suppose to do, I can help you with that.
Related
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 5 years ago.
Improve this question
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package plaindromenumbers;
import java.util.Scanner;
/**
*
* #author taha
*/
public class PlaindromeNumbers {
/**
* #param args the command line arguments
*/
int func1(int n)
{
if(n==1)
return 1;
return n*func1(n-1);
}
static boolean check=false;
int func(int no)
{
String a=""+no;
String reverse = new StringBuffer(a).reverse().toString();
if(a.equals(reverse))
{
if(!a.contains("0"))
{
System.out.println("hey");
check=true;
return Integer.parseInt(a);
}
}
// else
// {
func(no++);
if(check==true)
{
return 0;
}
return 0;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner in=new Scanner(System.in);
System.out.println("Enter testcase");
int testcase=in.nextInt();
while(testcase>0)
{
int a=in.nextInt();
PlaindromeNumbers obj=new PlaindromeNumbers();
System.out.println(obj.func(a));
testcase--;
}
}
}
Here is the problem:
func(no++);
Let's say no is 32. This will pass 32 to func and then increment it. Which means that you are once again calling the function with a value of 32. And when that function hits this line, it will, once again, pass that 32 to func. Hence, an infinite recursive loop.
You can fix this bug like this:
func(++no);
Now it will increase no before calling func. no will be 33, it will match its reverse, and all will be well.
You will need to return func(no + 1), this way your code will return the next palindrome number (which I assume is what you want your code to do?). check is not needed. Why include func1 when it's not even used by your code?
By the way, the stack overflow is caused by the infinite recursion of func.
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 5 years ago.
Improve this question
package com.company;
public class Main {
public static int add(int x, int y) {
return x + y;
}
/**
* public static int multiply(int x, int y) {
* int m = 0;
* for (int i = 0; i < x; i++) {
* m = add(m, y);
* }
*/
public static void main(String [] args){
add(0,2);
}
}
I want to:
Know the result for the addition of x + y and display it in screen. I have passed values for x and y in void main. Kindly Tell me how to know result. Thank You.
You need a main method. It does not matter what your class is called; it could be MyMain, or CoolTest, or any valid identifier. What matters is that you have a method with the following signature:
public static void main(String[] args) {
//Your code
}
Whatever you put into this method will be the code run when you run this class.
Without a main method, the code would be ambiguous; the JVM would have no way of knowing which method to run. The main method is the most common way of providing Java with an entry point into your code, telling it where to begin (there are other ways, but they are more complicated and probably not relevant to you at this point as a beginner). After all, what would it even mean to run your application? Should add be called, or multiply? And what values should be added or multiplied? The main method should hold the answer.
So, for a Uni project (UI programming), I'm writing a webpage with calculators for four separate strength training programs, all of which require separate calculations to produce a program. I'm fairly new to programming, and have never used JSP before. In fact, it was just today that I started doing the JSP tutorials in NetBeans.
I'm kind of at a loss here, not really sure how to go about doing this, so I'll start by outlining the way the training programs are supposed to work (I know this is a bit long-winded, but I figure I might as well put as much info in here as possible).
In all programs, the imaginary lifter inputs their best lifts, and the program then constructs a training routine for them.
One program is a linear progression, that just adds weight either 2kg or 5kg at a time for 12 weeks (Squat and DL 5kg, bench and OHP 2kg).
One follows the same basic principle, but with different lifts.
One is based on a popular training program known as 531. The program is constructed of 4 week mini cycles in which the weights go up over the course of four weeks. This is % based (as in week 1 would be 75% of the weight that the user had entered into the form, nest week is 85% etc.).
The last is a more complex olympic lifting program with various different percentages used. The user can choose between having a 3 day or 6 day a week program. The 3 day program is the same, but without the extra days (by which I mean the 3 day program uses three days that are the same as the six day variant).
Below is my java class (at this point). Since the programs utilize many of the same lifts, I thought it would save time to just have all of the lifts in the same source, and have the separate programs call them if they needed them. However, now that I've been thinking, since the programs require different calculations, maybe I should actually have different source files for all of the separate training programs?
The way it is currently set up is that the source file (below) just houses the variables, and my intention was to have separate HTML pages for each training routine, that contain forms into which the user inputs their data, which when prompted, moves the user to a JSP which shows them the result (i.e. their program).
package org.packageLifts.Lifts;
public class Lifts {
private int Squat;
private int Bench;
private int Deadlift;
private int OHP;
private int Snatch;
private int CleanJerk;
private int FrontSquat;
public Lifts() {
Squat = 0;
Bench = 0;
Deadlift = 0;
OHP = 0; /* Init all maxes to 0*/
Snatch = 0;
CleanJerk = 0;
FrontSquat = 0;
}
/**
* #return the Squat
*/
public int getSquat() {
return Squat;
}
/**
* #param Squat the Squat to set
*/
public void setSquat(int Squat) {
this.Squat = Squat;
}
/**
* #return the Bench
*/
public int getBench() {
return Bench;
}
/**
* #param Bench the Bench to set
*/
public void setBench(int Bench) {
this.Bench = Bench;
}
/**
* #return the Deadlift
*/
public int getDeadlift() {
return Deadlift;
}
/**
* #param Deadlift the Deadlift to set
*/
public void setDeadlift(int Deadlift) {
this.Deadlift = Deadlift;
}
/**
* #return the OHP
*/
public int getOHP() {
return OHP;
}
/**
* #param OHP the OHP to set
*/
public void setOHP(int OHP) {
this.OHP = OHP;
}
/**
* #return the Snatch
*/
public int getSnatch() {
return Snatch;
}
/**
* #param Snatch the Snatch to set
*/
public void setSnatch(int Snatch) {
this.Snatch = Snatch;
}
/**
* #return the CleanJerk
*/
public int getCleanJerk() {
return CleanJerk;
}
/**
* #param CleanJerk the CleanJerk to set
*/
public void setCleanJerk(int CleanJerk) {
this.CleanJerk = CleanJerk;
}
/**
* #return the FrontSquat
*/
public int getFrontSquat() {
return FrontSquat;
}
/**
* #param FrontSquat the FrontSquat to set
*/
public void setFrontSquat(int FrontSquat) {
this.FrontSquat = FrontSquat;
}
}
As I said, I am very much new to how this all works. My main questions are:
Should the actual calculations take place in the source file? If so, I would assume that all training programs should have their own sources. Am I right?
Taking the 531 program as an example, how would one go about printing four weeks of training routine with the weights being 75%, 85%, 95% and 50% of the input given, respectively?
Sorry for the excruciatingly long question. Any and all help is appreciated, in addition to my main questions.
Thank you in advance.
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 8 years ago.
Improve this question
I am trying to write a Tester and a class that can solve quadratic function.
If you are unfamiliar with quadratic function, or need a reminder, here is a quick link to it's Wikipedia article: http://en.wikipedia.org/wiki/Quadratic_function
My tester seems to work fine, however whenever I try to call the method, the program stops, and it doesn't display the output of the method (which is supposed to display the answer).
I am not skilled enough to find out if the error is within the class or the tester.
Tester:
/**
* A Tester to use to solve quadratic formula. Enter your values
* when prompted, and the answer will be displayed on screen.
*
* #author (Austin C.)
* #version (1.0.0)
*/
import java.io.*;
import java.util.*;
public class C_tester
{
public static void main (String args[])
{
Scanner kb = new Scanner(System.in);
System.out.println ("Enter the coefficents in the form of the following:\n1.A\n2.B\n3.C");
System.out.print("Enter the number for A:");
int a = kb.nextInt();
System.out.print("Enter the number for B:");
int b = kb.nextInt();
System.out.print("Enter the number for C:");
int c = kb.nextInt();
QuadraticFunction.quadratic(a,b,c);
}
}
Quadratic Function Class:
/**
* #Params: you must enter the coefficents, A, B, and C, and the program will calculate them to find the answer
* to a quadratic forumla. coefficents must be integers or doubles.
*
* #author (Austin C.)
* #version (1.0.0)
*/
public class QuadraticFunction
{
public void QuadraticFunction()
{
}
public static double quadratic(double a, double b, double c)
{
double topPos;
double topNeg;
double bot;
topPos = -b + Math.sqrt(Math.pow(b,2.0) - 4 * a * c);
topNeg = -b - Math.sqrt(Math.pow(b,2.0) - 4 * a * c);
bot = 2*a;
double ansPos = topPos/bot;
double ansNeg = topNeg/bot;
return ansPos + ansNeg;
}
}
Any help to find the error myself or find it for me is greatly appreciated. Also, if you find a more efficient way to do this, please share! I am always looking for more efficient ways to write code.
If the question is unclear, please say so and I can redo it in a more understandable way.
quadratic has return type double, and there are no print statements in the method. What this means is that it should return a value, but there is no reason that value should be printed. You can fix this by assigning the function result to a variable, and then adding a statement to print it to screen, like so:
double answer = QuadraticFunction.quadratic(a,b,c);
System.out.println(answer);
Also, you should note that a quadratic equation can have 2 real roots (possibly both the same), so you should be returning an array of doubles rather than a single double which is the sum of those 2 roots.
What do you expect it to print? You never tell the program to print anything. You can have it print the result to stdout by changing the last line in the tester to System.out.println(QuadraticFunction.quadratic(a,b,c)); if the program stops and there was no stack trace printed it means that it was executed succesfully
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 8 years ago.
Improve this question
I have a command line input as 0 1 2 and the main code can be seen below.
public class AppleStoreRunner {
public static void main(String [] args) {
//maximum size of queue
int qCapacity = Integer.parseInt(args[0]);
//number of simulation hours
int simHours = Integer.parseInt(args[1]);
//average number of customers per hour
int custPerHour = Integer.parseInt(args[2]);
AppleStore myStore = new AppleStore(qCapacity, simHours, custPerHour);
//Run simulation
myStore.simulation();
myStore.displayAcceptedCustomers();
myStore.displayServedCustomers();
myStore.displayWaitingCustomers();
myStore.displayTurnAwayCustomers();
}
}
How can I call the inputed command line arguments in the following class so that I may use the inputs in a seperate extended class? The code below is the class I am trying to create variables for the 3 inputed numbers.
public class AppleStore {
int qCapacity;
int simHours;
int custPerHour;
/** Constructor
* #param qCapacity The initial capacity of the queue to be used.
* #param simHours The number of hours that the simulation should run.
* #param custPerHour expected number of customers to arrive per hour.
*/
public AppleStore(int qCapacity, int simHours, int custPerHour)
{
qCapacity = AppleStoreRunner.main(args);
}
/**
* This methods performs a simulation of a store operation using a queue and prints the statistics.
* For every minute, the simulator 1) checks if there are new customers arriving; 2) adds the new customer into the waiting line or else records the customer who chooses to leave; 3) continues to help the current customer if the current customer is not finished yet, or else get the next person in the waiting line. The simulator starts at minute 0, and repeats every minute until it finishes the requested simulation time.
*/
public void simulation( )
{
System.out.println( "Average Waiting Time" + );
System.out.println( "Average Line Length" + );
/**
* print the info of all accepted customers
*/
}
public void displayAcceptedCustomers()
{
System.out.println("Customers accepted" + );
/**
* print the info of all served customers
*/
}
public void displayServedCustomers()
/**
* print the info of all waiting customers
*/
public void displayWaitingCustomers()
/**
* print the info of all turned away customers
*/
public void displayTurnAwayCustomers()
}
Because you did AppleStore myStore = new AppleStore(qCapacity, simHours, custPerHour); in your main method, all you need to do is to define a proper constructor.
public AppleStore(int qCapacity, int simHours, int custPerHour)
{
this.qCapacity = qCapacity;
this.simHours = simHours;
this.custPerHour = custPerHour;
}
As you declared the three instance variables as package-private, the subclasses will automatically see the existence of the three variables.
However, if you make the subclasses somehow immune to the changes to the superclass (I mean, the AppleStore), I recommend to add some getters for the three variables than can be called from the subclasses such as:
int getQueueCapacity() {
return this.qCapacity;
}
and to change the access levels of the three variables into private.