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


ADVERTISEMENT

Errors On Compiling But Not Showing In Eclipse?

May 6, 2015

I am watching BrandonioProductions on youtube. His videos are pretty good, but he doesn't seem to get errors where I get them. Here is an example.

public class compareUnequal {
public static void main(String[] args){
compareTwo();
}
public static void compareTwo(){
String x = "david";
String y = "Notdavid";
if (!x.equals(y)){
System.out.println("Not equal");
}
}
}

It runs ok, but asked me to proceed when there is an error, but I can`t figure out where the error is!

View Replies View Related

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

Writing A Method That Returns Words Without Four Letters Back To Main Method

May 27, 2014

I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.

Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.

import java.util.Scanner;
 public class censorProgram {
public static void main (String args[]){
Scanner input = new Scanner (System.in);
System.out.println ("How many words would you like to enter?");
int lineOfText = input.nextInt();
 
[Code] ....

I have to make new string array in the method and return words without four letters in the main method

View Replies View Related

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 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 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

Create A Method That Returns A New Array

Mar 4, 2014

I need to create a method that returns a new array containing the componentwise sum of its arguments(if length is the same). For instance, if the input arrays are {0,1, 2} and {2, 2, 3} then the output is {0+2, 1+2, 2+3}, i.e. {2,3,5}.If the input arrays have different numbers of elements, the method should return null.

I came with something like this, however i dont know how to make a copy of an array from two arrays. My code obviously wont compile. package whatever;

import java.util.Arrays;
public class hhhh {
public static void main(String[] args) {
double [] a = {1,2,3};
double [] b = {2,3,4};

[code]...

View Replies View Related

Method That Returns Largest Value Of Array Of Doubles?

Jan 26, 2015

Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.

Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.

package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list

[Code]...

View Replies View Related

Queues - IsEmpty Method Only Returns False

Jan 29, 2015

My isEmpty method only returns false. Is something wrong? I printed the empty and not empty for testing purposes.

//determines if there are any items in the queue
public boolean isEmpty() {
if (front == -1 && rear == -1) {
System.out.println("empty");
return true;
} else {
System.out.println("not empty");
return false
}
}

View Replies View Related

Static Method That Requires No Arguments And Returns No Value

Sep 30, 2014

I am doing a homework assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding. If someone could dumb down their response and tell me what I did wrong

This is what I supposed to do:

1.) Type the following shell for the class:

public class ParadiseInfo
{
}

2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:

public static void main(String[] args)
{
}

3.) Between the braces of the main()Method, insert a call to the displayInfo() method:

displayInfo();

4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:

public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}

This is what I attempted to do: I know it is wrong I am just clueless on where to put the code and why

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}

--- Update ---

I have also tried this:

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{

[Code]...

The very last attempt I only ended up with one error which is:

G:ParadiseInfo.java:3: error: invalid method declaration; return type required
displayInfo();
^
1 error

Tool completed with exit code 1

Java is very new to me and I have no clue on what I am doing wrong.

View Replies View Related

Swing/AWT/SWT :: GetGraphics Method Returns Null

Aug 23, 2007

The getGraphics() method in my constructor is returning a nullPointerException and I can't figure out why. This class is extending a JPanel and I thought calling super() would fix it because it makes sense that the JPanel would initialize its graphics object in the constructor, but that didn't fix it. The line that produces the error is in bold. I've narrowed it down to the getGraphics() method is returning null, so the next line that calls the graphics object is actually the one that produces the error.

int leftMargin = 2, rightMargin = 2, topMargin = 2, bottomMargin = 2;
int lineSpacing = 1;
int currentX = leftMargin, currentY = topMargin;
Font defaultFont, customFont;
public HTMLFrame(){
super();

[Code] ....

print(String) is a method I made that draws lines of text on the graphics object, similar to System.out.println(String) in the command prompt.

View Replies View Related

Method That Returns Positive / Negative Or Zero Depending On Sum Of Two Variables?

Dec 10, 2014

I'm taking a class in object oriented programming and we have a task to write a method that returns positive, negative or zero depending on the sum of two variables.

I've had a go at it and i've got to a certain point but i'm struggling to get past this on error - return outside method.

public class Test {
public int sumArgs;
public int arg1;
public int arg2;

[Code] ....

The error occurs on the line 'return "Positive";

View Replies View Related

Method That Returns Non-Numeric Values - Switch Statement

Feb 23, 2014

I am trying to write out a program that takes numerical input from the user and converts it to a date using the English month name. I am experimenting with the method of a "switch" statement without using the "break" clause. However, I seem to be missing something, as Eclipse is telling me I have a syntax error with my block. My curly braces seem properly placed. Also, I made sure to follow guidelines to make my code fit on the screen and remain easy to read.

import acm.program.*;
public class MethodsThatReturnNonNumericValues extends ConsoleProgram {
public void run() {
int month=readInt("Enter month number");
int day=readInt("Enter day");
int year=readInt("Enter year");

[Code] ....

View Replies View Related

Method That Returns Largest Object In Array Of Objects

Apr 4, 2015

This is what I have to create : Write a method that returns the largest object in an array of objects. The method signature is:

public static Object max(java.lang.Comparable[] a)

All the objects are instances of the java.lang.Comparable interface. The order of the objects in the array is determined using the compareTo method.

Write a test program in the main method that creates an array of ten Strings, an array of ten Integers, and an array of ten java.util.Dates, and finds the largest string (i.e. in the alphabetical order), Integer, and Date in the arrays.

Name your java class Max and your java file Max.java.

I am struggling a bit with this code as I am sure you can see, and am at loss. I have never used the compareTo method. Am I doing this right, or on the right track with my code?

public class Max implements Comparable {
public static Object max(java.lang.Comparable[] a) {
java.lang.Comparable tempObj = null;
for (int i = 0; i < a.length; i++) {
if (a[i].compareTo(tempObj) > 0)

[Code] ....

View Replies View Related

Write A Method That Returns Unicode Values Of A String?

Oct 25, 2014

I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.

View Replies View Related

Generic Method And Bound Type Parameter Errors

Jul 14, 2014

I am following this article [URL] .... till now I have made some code

This is my Interface

public interface Comparable<T> {
public int compareTo(T o);
}

And this is my class where I am using Bound Type Parameter on Generic Methods

public class GenericMethodBoundType {
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)

[Code] .....

What else I need to do both in main method and at what parameterized types I need to pass at the class?

View Replies View Related

Creating A Static Method That Requires No Arguments And Returns No Values?

Sep 28, 2014

I am doing an assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding.

This is what I supposed to do:

1.) Type the following shell for the class:

public class ParadiseInfo
{
}

2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:

public static void main(String[] args)
{
}

3.) Between the braces of the main()Method, insert a call to the displayInfo() method:

displayInfo();

4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:

public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}

This is what I attempted to do:

I know it is wrong I am just clueless on where to put the code and why

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}

--- Update ---

I also tried it this one and ended up with 1 error..

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}
}

View Replies View Related

Static Method - Take Three Boolean Inputs And Returns True If Odd Number

Jun 28, 2014

Write a static method odd() that takes three boolean inputs and returns true if an odd number of inputs are true, and false otherwise.

View Replies View Related

Java Game Errors - No Suitable Method Found For Intersect JButton

Apr 9, 2014

I am receiving two errors that I'm not sure how to fix. The first is illegal start of expression at line

"public void actionPerformed(ActionEvent e)"

and no suitable method found for intersect JButton at line

"if(blocker01.getBounds().intersects(r_wall))" .
 
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 public class gamePanel01 extends JPanel implements KeyListener {
character ch1 = new character("Hero");

[Code] ....

View Replies View Related

Sort String List Alphabetically With CompareTo Method / If Else Statements - Logic Errors

Oct 6, 2014

I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .

The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.

For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.

(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)

System.out.println("Enter the first statement: ");
//input.nextLine();
string1 = input.nextLine();
System.out.println("Enter the second statement: ");
string2 = input.nextLine();
System.out.println("Enter the third statement: ");
string3 = input.nextLine();

[Code] ....

View Replies View Related

Java Class Date Method Showing Wrong Year

Dec 31, 2013

We use a Java program to read an XML file and put its content into a database. We also use Date() to get the current date and insert it into a field in database with content from XML file. Everything worked flawlessly until today, the last day of 2013. We are getting 2014 for the year instead of 2013!!! System date shows correct year, so this must not be an issue.

View Replies View Related

Get Background Image Showing Other Two Are Not Showing On Screen

Jan 20, 2015

public void paint(Graphics g) {
g.drawImage(Background, bg1.getbX(), bg1.getbY(), this);
for(int i=0;i==400;i+=10){
g.drawImage(block1,fg1.getBlockX(),fg1.getBlockY(),this);
g.drawImage(block2,fg1.getBlockX(),fg1.getBlockY()+10,this);
}
}

This is what im dealing with right now. When i click run i only get the background image showing the other two are not showing on the screen.

View Replies View Related







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