Creating Static Methods That Accepts Arguments And Returns A Value
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)
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."); } }
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:
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?
If i try to make it public void than it say can't call non-static methods inside static(main).if i try to put it into new class and then call it after i fail input it goes into infinite loop.
package Experimentation; import javax.swing.*; import java.awt.event.*; public class SimpleGUI1B implements ActionListener { JButton button; public static void main(String[] args) { SimpleGUI1B gui = new SimpleGUI1B(); gui.go();
[code]...
This is a program from Head First Java! since main is static it shouldn't be able to call non-static methods because statics do not use any instance variable values but in the above program we're call a non-static method go() how is it possible?
I have a test that covers Objects & Classes, Importing Classes and Polymorphism. One of the essay questions will be: Explain two ways to pass arguments to methods and give examples. I was reading the book and found Pass by Value and Pass by Reference. Is this the two ways to pass arguments?
how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??
example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??
class test{ public static void main(String [] args){ Scanner input = new Scanner(System.in); //create new Scanner object //for input int number1; int number2;
1. Create a class that will bundle together several static methods for tax computations. This class should not have a constructor. Its attributes are
• basicRate—the basic tax rate as a static double variable that starts at 4 percent - a private static method
• luxuryRate—the luxury tax rate as a static double variable that starts at 10 percent - a private static method
Its methods are
• computeCostBasic(price) —a static method that returns the given price plus the basic tax, rounded to the nearest penny.
• computeCostLuxury(price) —a static method that returns the given price plus the luxury tax, rounded to the nearest penny.
• changeBasicRateTo(newRate) —a static method that changes the basic tax rate.
• changeLuxuryRateTo(newRate) —a static method that changes the luxury tax rate.
• roundToNearestPenny(price)—a private static method that returns the given price rounded to the nearest penny. For example, if the price is 12.567, the method will return 12.57.
My code:
public class TejvirThindCh6Ex1 { /** * @param args the command line arguments */ //Atributes private static double basicRate = 4.0; private static double luxuryRate = 10.0; public static double computercostbasic(double price)
Declaring the method as static precludes one from using any sort of object oriented programming, thus the method cannot access instance fields of the object if it needs to.
I created two short classes to sort of find out what this meant, but I feel I do not understand it well enough.
Test class (main):
package votek; public class Test { public static void main(String[] args) { SomeMethod(); } public static void SomeMethod() { Character character = new Character(); character.totalLevel = 50; System.out.println(character.totalLevel); } }
Character class:
package votek; public class Character { private int level = 0; public Character() { level = 50; } }
OR
Does it mean that making a static method in the class with private instances will prevent the method from using the private instances?
I am wondering if there is a way in jave to use enums WITHIN a class (without creating a separate enum class) without using private static final. Something like as folows:
class My Class { myEnum {ACTIVE, INACTIVE, PENDING}; }
public class demo { Public class static void main(String[]args) { //Creating a variable that will be a reference to the object Peoples person_one;
[Code] ....
I have assembled this code below that has a void method which will creat a new object. Problem I encounter is that in
Create_object(person_one);
the person_one has an error saying not initialized. I'm jus trying to learn on my own ways here and practice so may know what's wrong with this? I know I can use a return object from methods but what about this approach?
I want to make a program where users are prompted to enter a username and a password and have these two values create a new instance of the Object User. But I'm not sure where to start.
import java.util.Scanner; public class Main { public static void main(String[] args) { createUser();
[Code] ....
how to take username + password and put it into an object.
So I have this class containing all my Enum types, also including methods for fetching Enum types based on their title attribute:
abstract class Enums { static private Landmass getLandmass(String name) { for ( Landmass l : Landmass.values( ) ) { if(l.title.equals(name)){ return l;
[Code] .....
The second method (getAttribute) is created by copy-paste and I also need to repeat this exercise with several other types of Enums. This seems like a waste of code however.
Instead, I thought I'd create a generic method for fetching any type of Enums. As far as I could follow the tutorial @ Oracle I need a generic class for this. Thus the EnumHelper class:
abstract class EnumHelper<T> { private T getEnum(T type, String name) { for ( type t : type.values( ) ) { if(t.title.equals(name)){ return t; } } return null; } }
This, however, doesn't compute:
horoscopeEnums.java:234: error: cannot find symbol for ( type t : type.values( ) ) { ^ symbol: class type location: class EnumHelper<T> where T is a type-variable:
[Code] ....
2 errors
To be honest I haven't been able to make much sense of the documentation on generics, thus its no surprise I'm stuck.
I thought you can only create a new object using private implementations and then using a constructor to set your arguments inside the parameters of the constructor to the instance variables but how come he created an object without any private implementations and just methods inside the constructor.
import javax.swing.JFrame; public class MyWindow extends JFrame { public static void main(String[]args){ new MyWindow(); } public MyWindow(){ setSize(500,500); setVisible(true); setTitle("MyWindow"); } }
I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.
package fractions; import java.util.Scanner; import java.util.Arrays; public class FractionArrays { public static void main(String[] args) { Fraction completeFraction = new Fraction(5,6);
[Code] ....
I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?
public String firstName() Returns the customer's first name public String lastName() Returns the customer's last name public double balance() Returns the customer's account balance
Finally I need to create a driver to test my class. And create several accounts and verify that the first name, last name, and balance methods work properly. This is my code below.. I don't know if I did it right.
public class BankAccount { String firstName, lastName; double balance; public BankAccount(String firstName, String lastName, double balance) {
I have an Abstract Class called GameColorEffect which contains a number of non-static Inner Classes that extend their Parent Class, GameColorEffect. I want to be able to create instances of the Inner Classes, however my IDE, eclipse, prompts me with the error:
No enclosing instance of type GameColorEffect is accessible. Must qualify the allocation with an enclosing instance of type GameColorEffect
And eclipse shows me a possible solution which is to turn the Inner Classes to static, this would allow me to create instances, but not really. This is because using methods from the static Inner Classes that change values in the Inner Classes will do this for every instance of the same Inner Class which is literally like a single instance. However, I want these Inner Classes to be individual with their values and still be able to use them outside as instances. I've found out a possible solution, which I'm not sure works like I want it to:
Java Code : GameColorEffect = new GameColorEffect.ExampleEffect(); mh_sh_highlight_all('java');
However, this is in-compact because sometimes all I need is to use just a method like:
Java Code : new GameColorEffect.ExampleEffect(intensity).applyEffect() mh_sh_highlight_all('java');
And another solution that I already knew prior was that I could make the Inner Classes proper classes not inside of the GameColorEffect class, but this is also in-compact because I will have to have so many classes for the so many effects that I have.
So basically I have to create a java program that accepts a string and does the following:
1.) Insert a blank space before capital letters 2.) Convert any capital letters into lower case except the first letter(if it is capital).
So if the user enters "HiMyNameIsJohn", the result should be "Hi my name is john". I am having problems inside my changeMe method. Everything works fine including putting a blank space in between upper case words. So from the previous example, my code would show "Hi My Name Is John". I am having issues making the code to convert the upper case letters into lower case. Im pretty sure the code should be right after the str.insert(i, " "). But I do not how to code it. Ive thought about Character.toUpperCase(ch); but doesnt work.
Java Code:
import java.util.*; public class WordSeparator { public static void main(String[] args) { String word1, word2; Scanner userInput = new Scanner(System.in);
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one.For example, hopscotch(3); should print:
How do I code this without having the need to use iterator? Code a Java method that accepts an ArrayList of integers and an integer. The method should delete all elements in the array list exactly divisible by the integer and return the number of remaining elements.
Write a class that accepts a user's hourly rate of pay and the number of hours worked. Display the user's gross pay (gross pay = hours worked * hourly rate), the tax withheld (tax withheld = gross pay * tax rate) and the net pay (net pay = gross pay - tax withheld).Use a named constant for storing the tax rate of 0.15
Here is my Code:
import java.util.Scanner; class Tutorial { public static void main(String[] args);