Passing String [] args values to another class [closed] - java

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.

Related

"StackOverflow" error when using Recursion in Java [closed]

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.

trying to call a method within a method [closed]

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 6 years ago.
Improve this question
Trying to call a method within a method.
public static void main(String[] args) {
int sq,cu=0;
//user input 1
sq=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be squared"));
//user input 2
cu=Integer.parseInt(JOptionPane.showInputDialog(
"Enter value to be cubed"));
//results
JOptionPane.showMessageDialog(null, sqd(sq));
JOptionPane.showMessageDialog(null, cbd(cu));
}
public static String sqd(int sq){
int sqd=sq*sq;
//sq computation
return sq+" squared is "+sqd;
}
public static String cbd(int cu,int sqd){
int cbd;
cbd=sqd*cu;
//cu computation
return cu+" cubed is "+cbd;
} }
calling sqd value in cbd, but
JOptionPane.showMessageDialog(null, cbd(cu));
prevents me from doing so, it always gives me an error when I run it.
your cbd(int cu, int sqd) method needs 2 input parameters , you are calling it with only one parameter cbd(cu)
two choices :
1 - rewrite your cbd method with 1 parameter then you can call it using cbd(cu);
public static String cbd(int cu){
return cu + " cubed is " + (cu*cu*cu);
}
2 - write it's second parameter while you are using it :
cbd(cu,cu*cu);
Your cbd method, as it's currently defined, takes two arguments - cu and sqd. If you want to keep the way your main calls it, you need to rewrite it with just one argument:
public static String cbd(int cu){
int cbd = cu * cu * cu;
return cu + " cubed is " + cbd;
}

A couple small basic questions making issues [closed]

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.

Calculators for four weight training programs in JSP: need some guidance

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.

How to get system (BlueJ) to allocate unique number sequentially [closed]

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
/**File class */
public class File {
// The File name.
private String name;
// The date file created.
private String date ;
// The type of file - audio, image, video, doc.
private String type;
// The size of the file - 2MB, 2KB etc.
private String size;
/**
* Create a file
*/
public File(String Filename)
{
name = Filename;
date = ("MM/dd/yyyy");
type = ();
size = ();
}
/**
* Return the name of a File.
*/
public String getName()
{
return name;
}
/**
* Update system number when called to the output terminal
*/
public void Systemnumber ()
name.increment();
if(name.Filename() == 0) { // it jus rolled over !
i.increment();
}
updateDisplay();
}
/**
* Print File description to the output terminal
*/
public void updateDisplay ()
public String s = String.format ("%02d", i); // gives you "001"
for (int i = 001; i < 1000; i++) {String sequence = String.format("%02d", i); }
{
return (i + "", '$this.Name + " " + $this.date + " " + $this.type + " " +$this.size; }
/**
* This method is called everytime Increment the return value one, rolling over to zeor if the limit is reached
*/
public void updateDisplay ()
{ displayString = name.
(
value = (value +1) % limit;
)
}
Seeing as you're using BlueJ, I'll assume it's homework, so I won't answer the question as such, just give some pointers.
You need to store a counter in the File object, but, that counter needs to be shared between instances of the object. This counter can be used at construction time to get the number for each individual instance of File, which can then be stored for the instance in question.
What you are missing in your new update is the sequence number. You should store this in a static variable (so that it is shared between all instances of File) and then assign its current value to a member variable in the constructor, before incrementing the static variable for the next instantiation.
You will need to add a private static integer, which you can call it whatever you want. I will call it numberOfFileInstances. In your constructor you will need to increment numberOfFileInstances by one.
Here is my example:
public class File {
private static int numberOfFileInstances = 0;
public File() {
File.numberOfFileInstances++;
}
}
Since your using BlueJ you'll be easily able to see that each time you create a new file object the numberOfFileInstance will be incremented by one. In BlueJ initialize 2 (or any number you would like grater than 1) File objects and double click on the object to bring up the inspector. Click on the "Show static fields" button and you should see private int numberOfFileInstance and the count of however many object you initialized.
If you need a sequence counter, you might want to consider using a static integer which you increment for every file added.
I'm not sure I understand what you want but it sounds simple:
public String toString() {
return this.Name + " " + this.date + " " + this.type + " " +this.size;
}

Categories

Resources