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
ADVERTISEMENT
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
Oct 7, 2014
This is titled "Creating static methods that accepts arguments and returns a value". I think that I understood everything about this except for the very bottom part of the code. I wasn't really sure where to put it. From the errors that I am seeing, I know which line is giving the errors but I'm not sure what is wrong with it.
import java.util.Scanner;
public class ParadiseInfo2{
public static void main(String[] args){
double price;
double discount;
[Code] ....
Errors:G:ParadiseInfo2.java:29: error: illegal start of expression
public static double computeDiscountInfo(double pr, double dscnt)
^
G:ParadiseInfo2.java:29: error: illegal start of expression
public static double computeDiscountInfo(double pr, double dscnt)
[Code] ....
6 errors
Tool completed with exit code 1
View Replies
View Related
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
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
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
Jun 9, 2014
A method is declared to take three arguments. A program calls this method and passes only two arguments.Will it take the third argument as
1 null
2 void
3 zero
4 Compilation error
View Replies
View Related
Jan 24, 2014
I just kinda get stuck when it comes to passing values into constructors, using main method or static method functionality. In theory i kind of understand how it work but when i type it, it's totally different! I have to have a junit test too but i guess i could do that in the end.
I have attached the assignment. So, how to proceed with this:
public class Flight {
int flight_number, capacity, number_of_seats_left;
String origin, destination;
String departure_time;
double original_price;
[Code] ....
View Replies
View Related
Jun 15, 2014
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.
Here is the code:
Java Code:
import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");
[code]....
View Replies
View Related
Feb 19, 2011
I thought static methods could never use instance variables, because they wouldn't know which instance to look at.
From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."
Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?
View Replies
View Related
Mar 26, 2015
This is my code inside the method:
@Post
public static String getDetails(Representation entity) throws Exception {
String customerId = getQuery().getValues("cus_id");
}
I use this code in Restlet Representation. I try to get the value from the Request API. But I am facing the problem as "Cannot make a static reference to the non-static method getQuery() from the type Resource".
View Replies
View Related
Jun 27, 2014
From what i understand static methods should be called without creating an instance of the same class . If so why would they return an instance of the same class like in the following : public static Location locateLargest(double[][] a) , the Location class being the same class where the method is defined . I don't understand this , does it mean that every field and every method in the class must be static ? Meaning that you cannot have instances of the class because everything is static . Or it's just a mistake and the class Location cannot have a static method: public static Location locateLargest(double[][] a) ?
View Replies
View Related
Apr 26, 2015
I can't figure out what this error message "Cannot make a static reference to the non-static method getEndUserCharge(long, long, long, long) from the type UpdateUserWS" actually means.
The error is coming from:
public void updateDetailsPackage() {
some unrelated code
long zero=0;
double endUserCharge=0;
endUserCharge = UpdateUserWS.getEndUserCharge(long zero, long zero, long zero, long zero); <-------- error is here
[Code] ....
View Replies
View Related
May 26, 2015
Alright, I have two classes, this one
public class Player {
private String player;
public String getPlayer() {
return player;
}
private int strength;
private int defense;
[Code] .....
However, it says that under Player.getPlayer() that it 'Cannot make a static reference to the non-static method'.
View Replies
View Related
Oct 28, 2014
Why I can create an Instance of a class that contains non static variables within the static main method ?
This program runs fine
import java.util.*;
public class Test{
public int j;
public static void main(String[] args) {
Test test1=new Test();
System.out.println(test1.j);
[Code] .....
View Replies
View Related
Jul 27, 2014
I'm working on a banking program that is supposed to use 3 classes (Account-base class, CheckingAccount, and SavingsAccount) and several methods to display the banking information (ID, balance after a withdrawal and deposit, and the annual interest rate). This should be a pretty simple program, but I'm getting hung up on one portion of it. I'm getting some compiler errors, all of which deal with non-static variables being called from a static context (I'll also post these errors following the code). Up until the middle of last week, we just declared everything as static, but that's changed and I'm having trouble figuring out when to and when not to use static when declaring my methods, hence the compiler errors.
import java.util.Date;
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();
[Code] ....
Here are the compiler errors I am receiving:
Compilation completed. The following files were not compiled:
6 errors found:
File: C:UsersHiTechRedneckDesktopSummer II 2014Computer Programming PrincipleProgram 5CheckingAccount.java [line: 7]
Error: non-static method getId() cannot be referenced from a static context
[Code] .....
View Replies
View Related
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
Nov 18, 2014
I had a TestColor class which contained methods to change hue, saturation, brightness, red, green, blue of TestColor's instances but also had static methods which take in an additional parameter for an instance of TestColor and returns the affected instance of TestColor. Now instead of having one method for every possible color effect to be applied to an image, how can I have one method that takes in an Image parameter, a static or non-static method reference from TestColor parameter and lastly an intensnity value parameter. This is so that I can make an affectedImage object instance inside the method and a Graphics2D object for drawing to each pixel of the new image, now I have one for loop and one nested for loop for the x and y pixels of width and height of the old image and inside the nested for loop I'd create a TestColor by calling getRGB on the image's pixel. Then I would apply the static or non-static method reference somehow to change the color with the intensnity value and after applying it draw to the new Image with Graphics2D. How to would I parametize a method reference and be able to use it in such way?
View Replies
View Related
Jul 14, 2014
One class having two method one as static n another as non-static, 2 threads are there t1 is accessing the static method and t2 the non-static method is it possible n both are sharing the same object.
I now we have two kinds of lock one is object level lock and another is class level lock
View Replies
View Related
Feb 1, 2015
I am trying to compile the following code where the entry point is Main.java but i am getting the following error "Error non-static method add(int[]) cannot be referenced from a static context in Java " ....
If i add the keyword static in add method of A.java it works properly......
Class Main.java code
package com.company;
public class Main {
public int [] a= new int[]{1,2,3};
public static void main(String[] args) {
Main n= new Main();
[Code]...
Both the classes are in same package
View Replies
View Related
Mar 24, 2014
How to get used to program control statements. This is an attempt to write a program that returns the number of days between two dates. Sadly, it give incorrect values.
public class DaysBetweenDates {
public static boolean isLeapyear(int year) {//This method will determine if a year is a leap year or not
boolean isTrue=false;
if(year%4==0 && year%100!=0) isTrue=true;
else if(year%400==0) isTrue=true;
return isTrue;
[Code] ....
View Replies
View Related
Oct 6, 2014
class Test
{
static Test ref;
String[] arguments;
public void func(String[] args)
{
ref.arguments = args;
[code]...
This code run perfectly.But the static method is acessing a non static method which was supposed to be not possible.
View Replies
View Related
Jun 24, 2014
We know that a non static method can not be directly refrenced from static method but in the example given below there is no such error.Why?
class A
{
private void display()
{
System.out.println("In A");
}
public static void invoke (A x)
{
x.display();
[code]....
In above case a non static method display() is being invoked from static method invoke ()but it is not giving error.why?
View Replies
View Related
Feb 5, 2015
I want to take command line arguments and pass them to a paint method. This is a test program that will just draw some equations. How can I get the input array clinputs[] to be used in public void paint( Graphics g) ?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LinePlot extends JFrame {
public LinePlot() {
super( "Line Plot" );
setSize(800,600);
[Code] .....
View Replies
View Related
Jun 7, 2014
I am having a bit of difficulty understanding/Using FileWriter. Now by my understanding in the API FileWriter allows to use write at the end of your file, if you have text in there already, correct?
I have a file with people info in it. And I created a GUI which allows people to input that data.
Java Code:
File file = new File("People.txt");
FileWriter fw = new FileWriter(file);
People p = new People(firstName, lastName, email, sex));
fw.append(p);
fw.close() mh_sh_highlight_all('java');
Now, I keep getting an error on the fw.append(p)
Java Code:
The method append(CharSequence) in the type Writer is not applicable for the arguments(People) mh_sh_highlight_all('java');
View Replies
View Related
Sep 8, 2013
Which is more efficient static block or static method ?
View Replies
View Related