Trying To Calculate Fine Using Method Calls

Jul 3, 2014

I am trying to calculate a fine in a PoliceOfficer object with method calls to a ParkedCar and ParkingMeter object. The word problem is:

The fine is $25 for the first hour or part of it and $20 for every additional hour of part of it.

My code is:

public class PoliceOfficer
public static final int PARKING_FINE1 = 25;
public static final int PARKING_FINE2 = 20;
public static final int NUMBER_OF_MINUTES_OVER_PARKED = 60;
public double calculateFine(){
double calculateFine = 0;

[Code] ....

obviously the fine is not calculated correctly but I'm not sure how to proceed from here...

View Replies


ADVERTISEMENT

Altering Parent Method Behavior By Overriding Method It Calls

Apr 21, 2014

I have two classes (Daughter and Son) that contain some very similar method definitions:

public class Family {
public static void main(String[] args) {
Daughter d = new Daughter();
Son s = new Son();
d.speak();
s.speak();

[Code] .....

Each of those classes has a "speak" method with two out of three lines being identical. I could move those into a parent class, but I need each of the child classes to continue to exhibit its unique behavior. I'm trying the approach below, which replaces the unique code with a call to a "placeholder" method that must be implemented by each child class:

public class Family {
public static void main(String[] args) {
Daughter d = new Daughter();
Son s = new Son();

[Code] .....

This works and moves the shared code from two places (the Daughter and Son classes) into one place (the new Mother class, which is now a parent class of Daughter and Son). Something about this feels a bit odd to me, though. It's one thing for a child class to override a parent class's methods to extend or alter their behavior. But, here, I've implemented an abstract method in the parent class to alter what happens when the parent class's method (speak(), in this case) is called, without overriding that parent class method itself.

View Replies View Related

How To Trace Method Calls

Apr 1, 2015

Consider the two simple Java classes below:

class Computer {
Computer() {
System.out.println("Constructor of Computer class.");
}
void On() {
System.out.println("PC turning on...");

[Code]...

After the program run, how do I trace (1) which object call which method (2) and how many times?

View Replies View Related

Inheritance Method Calls

Jun 25, 2014

If i have 2 classes, Top and ClassB which extends Top

public class Top {
String variable;
public Top(){
setVariable();
}
void setVariable(){
variable = "variable is initialized in Main Class";

[code]....

So what is happening when ClassB inherits from Top?I know that the B constructor is calling super, so does that mean its calling setVariable (in Top?) but as its overridden in ClassB, then that is whats being called and setting the String variable?

View Replies View Related

Using Consecutive Method Calls On A Single Object

Mar 24, 2014

I don't remember where but I faintly recall one of the oracle docs examples (dealing with GUI basics I think) having something similar to

button.add(lbl).addIcon().addActionListener(al);

but only in the aspect of using .method.method.method

The reason I'm asking is because I want to shorten this

int j = 0;
for(int i = 0; i <= 6; i+=3){
pnl[j+2].add(lbl[i+5]);
pnl[j+2].add(lbl[i+6]);
pnl[j+2].add(drop[j]);
pnl[j+2].add(lbl[i+7]);
pnl[j+2].add(btn[j]);
j++;
}

Into something like this

int j = 0;
for(int i = 0; i <= 6; i+=3){
pnl[j+2].add(lbl[i+5])
.add(lbl[i+6])
.add(drop[j])
.add(lbl[i+7])
.add(btn[j]);
j++;
}

I might be way off though... but I'm just wondering under what circumstances would this be possible

When I try it in Eclipse a little notification appears that says...

"The method add(PopupMenu) in the type Component is not applicable for the arguments (JLabel)"

When I google 'Component' I see it appears to have something to do with Serialization (which I'll need to look into)

View Replies View Related

Basic Inheritance And Static Method Calls

Jun 25, 2014

I think its a standard concept but just not getting it. I have 3 classes:

1) Base class
2) Derived class, which extends base
3) TestClass

public class Base {
public void display() {
System.out.println("Display method in Base");
}
}
class Derived extends Base {
public void display() {
System.out.println("display method in Derived");

[Code] .....

So if i run this, my results are:

Display method in Base
display method in Derived
display method in Derived

Thats cool, no problems there, but if i change my Display method in Base and Derived to static methods then the results become:

Display method in Base
display method in Derived
Display method in Base

So why oh why does obj3.display() now print "Display method in Base"?

(and yes I know the calls should be static calls, but for sake of arguments -- or is that the whole problem, that technically obj3.display() is an incorrect call as it can only be Base.display() or Derived.display() when display() is a static method ).

View Replies View Related

Using Method Calls With Returns But It Keeps On Showing Errors

Oct 12, 2014

I am trying to use method calls with returns but it keeps on showing errors. The errors say class, interface, or enum expected. I realize this error occurs if there is issue with declaring class - but i can't seem to find the error. I will post the code that shows error.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FuelCost extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);

[Code] ....

The error states:

C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:290: error: ';' expected
public double calculateCostToFillTank()
^
C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:292: error: class, interface, or enum expected
return (averageMilesPerTank / milesPerGallonRating) * currentPricePerGallon;

[Code] ....

12 errors

Tool completed with exit code 1

View Replies View Related

Using Synchronized With Static Method Calls Used In Multiple Threads?

Jun 30, 2014

I have a multithreaded application. I have a Logger class with static methods that I use across threads. Would it behoove me to add the synchronized keyword to the static methods of the Logger class since I use this class statically in different threads?

View Replies View Related

Recursive Method - Calculate Greatest Common Divisor Using Euclidean Method

Apr 29, 2014

Consider the following recursive method that calculates the greatest common divisor using Euclidean method.

PHP Code:

public static int GCD ( int x , int y )
{
    if ( y == 0 )                        
        return x;
    else if ( x >= y && y > 0)
        return GCD ( y , x % y );
    else return GCD ( y , x );  


Trace the above method for x=32 and y=46

View Replies View Related

Networking :: Ethernet Works Fine But Not Wireless

Jan 30, 2014

My application with Oracle Database only works with Ethernet, I tried to connect via wireless without any response. When I run the command netstat the port state is WAIT TIME and then be closing. The clients machines run Windows 7 and 8 and the server run Suse Linux Enterprise Server 10 SP2, if the client machine run XP all works fine. I disabled the firewalls in the both sides.

View Replies View Related

Applets :: Code Works Fine With All Browsers Except CHROME

Mar 26, 2014

i am calling a method of my applet from within the javascript. My code works fine with all browsers except CHROME.i get this exception in browser = "Uncaught TypeError: Object #<HTMLObjectElement> has no method 'loadComplete' mainControl.js:6". i called this method on the "onload" of <body> tag.

View Replies View Related

Calculate PI Using Monte Carlos Method

Aug 10, 2014

I've been looking at the calculating PI using Monte Carlos method. All of the codes request an input from the user to be able to determine PI. However, I want the program to print out only 10, 100, 1000, 10000, 100,000 and 1,000,000,000 for PI.

Here is the link to the original post ...

View Replies View Related

How To Calculate Remove Method Complexity

Feb 1, 2015

I need to figure out how to calculate the remove method complexity in the worst best and average case same as the insert method

public void insert(String Word)
{
size++;
int pos = myhash(Word);
WordNode nptr = new WordNode(Word,null);
if (table[pos] == null)
table[pos] = nptr;

[Code] .....

View Replies View Related

How To Add A Method To Calculate Mean Of Length Of Each Word Output

Mar 6, 2014

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Assignment extends Applet implements ActionListener {
TextArea textInput = new TextArea(); // user input
Button analyzebutton = new Button("Analyze");
Button resetbutton = new Button("Reset");
Label lbloutput = new Label ("Please enter text into the textbox!");

[Code] .....

View Replies View Related

Java Program Method To Calculate Distance

Feb 20, 2015

Write method distance, which calculates the distance between two points (x1, y1) and (x2, y2). All numbers and returned values should be of type double. Incorporate this method into an program that enable the user to enter the coordinates of the points, then calculate and display the distance by calling the method –distance.

I've tried numerous times to make it work and I'm on the right path, however I'm missing some things in the code to make my results look like this later on, which I've attached onto this post.

View Replies View Related

Method From Main - Calculate Weight On Different Planets

Nov 8, 2014

I have to write a program to calculate my weight on different planets and to do that, I have to read in the surface gravity of all the planets from a file using a separate method from main

public static double[] readGravity() throws IOException
{
double[] surfaceGravity = new double[8];
Scanner readFile = new Scanner("gravity1.txt");
int i = 0;
while (readFile.hasNext()) {
surfaceGravity[i] = readFile.nextDouble();
i++;
}
return surfaceGravity;
}

I get this message in the while loop when I try to run the program:

java.util.InputMismatchException;
null (in java.util.Scanner)

View Replies View Related

Write A Program That Will Call A Method To Calculate Function

Dec 3, 2014

I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:

x f(x)
1 -15
2 -12
3 -7
4 0
5 9
6 20
7 33
8 48
9 65
10 84

I guess it also has to print this output in a table even though my professor never mentioned it. Usually

public class Lab12 {
public static int f(int x)
{
return x*x-16;

[code]....

View Replies View Related

Modify DrawShape Method To Calculate Distance From Starting Point

Mar 13, 2015

I need to modify the drawShape method to calculate the distance from the starting point (the diameter) of any shape regardless of how many sides I give it, but I have absolutely no clue where to begin with this. The ultimate goal of the program is to calculate the value of pi using the shape that is drawn.

Java Code:

public class PiTurtle extends Turtle
{
private double mySize;
private int mySides;
private double diameter = 0;

[code]....

View Replies View Related

Loop That Calculate Distance Traveled - Why Java Not Printing Method Results

Apr 13, 2014

I am trying to write a loop that calculates the distance traveled (distance = speed * time). It should use the loop to calc how far the vehicle traveled for each hour of time. My program asks for hours and then mph but its not calculating time * speed. Here is my code.

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
System.out.println("Enter Hours Traveled ");
int hoursTraveled = input.nextInt();
System.out.println("Enter MPH ");
int mph = input.nextInt();
 
[Code] .....

View Replies View Related

Method Parameters - Two Fields Of Type Double / Calculate Distance To Another Point

Oct 18, 2014

I've just been having a go at an exercise where I have to create and use a class called Point, with two fields of type double. I have to create some methods for it, one of which is a distanceTo(Point) method, that calculates the distance to another point. I've tried to keep the distanceTo(Point) method short so have created some other methods to use within the method. My question is about the getDistance() method that I've made. As you can see below, I've given it two parameters, which are references to values within two Point objects (this.x and otherPoint.x).

double distanceTo(Point otherPoint) {
double distanceX = getDistance(this.x, otherPoint.x);
double distanceY = getDistance(this.y, otherPoint.y);
return calculateLength(distanceX, distanceY);
}

View Replies View Related

Error While Running In Command Prompt Only - In Eclipse Running Fine

May 19, 2014

This is my program: RemoteXMLRead.java

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.apache.commons.io.filefilter.WildcardFileFilte r;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

[code]....

It is working fine when i run in Eclipse, but is giving error when i run in cmd.. What i need to do to over come this..

View Replies View Related

How Recursion Calls Are Made

Jun 5, 2014

How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in

if(leftmax>rightmax){
return leftmax;}
else
return rightmax;

Here's the code:

public class Maximum{
public static int max(int[] a,int low,int high){
int mid,leftmax,rightmax;
if(low==high)
return a[low];

[Code] ....

firstly leftmax=max(a,0,4)

Then what is the next line executed?Is it rightmax=max(a,5,8).

After this is it leftmax=max(a,5,6)

rightmax=max(a,7,8)

I tried to understand what these recursion calls by writing them down.But I somehow make a mistake.

View Replies View Related

How To Trace Methods Calls

Mar 31, 2015

Consider the two simple Java classes below:
 
class Computer {
  Computer() {
  System.out.println("Constructor of Computer class.");
  }

[Code]...

After the program run, how do I trace (1) which object call which method (2) and how many times?

View Replies View Related

EJB / EE :: How To Obtain 2 Different SLSB By Two Successive Calls

Oct 4, 2014

I use jboss eap 6.2 under eclipse.I perform a tutorial on EJB and I want to show the difference between a stateless session bean and a statefull session bean. I want demonstrate that the first one keep its state (if it has one) but that 2 successive don't send back necessarily the same SLSB : it send back the SLSB that is ready in the pool. But I don't suceed in obtaining 2 different SLSB by 2 successive call. The system always send me back the same SLSB.

View Replies View Related

Encode Getmessage Calls In The Code?

Apr 21, 2015

Im trying to HTML/JavaScript ecode the getmessage calls in the code listed below. The code below is a snippit from a program which is used to handle a failed authentication attempt.

if(logger.isDebugEnabled())
logger.debug("Authentication Failure: " + exception.getMessage());
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Authentication Failed: " + exception.getMessage());

View Replies View Related

EJB / EE :: Pass By Reference Not Working Between Remote Calls

Mar 11, 2014

i'm having issues with pass by reference between remote EJB method calls. Below is what i'm trying to do. The EJB method calls are made using remote (EJBObject) interface.

insertEmployeeRecord(Employee employee) {
employeeEJB.insertEmployee(employee);
empDetailsEJB.insertEmpDetails(employee);
}
insertEmployee(Employee employee)

[Code] ....

All the EJBs have below configuration in the weblogic-ejb-jar.xml file.

<enable-call-by-reference>true</enable-call-by-reference>

I'm using Weblogic server 10.3.0.4 and JDK160_21.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved