The static method...should be accessed in a static way [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public class Main {
public static void main(String[] args) {
Person Gunnar = new Person();
Person Sven = new Person();
Dog Doggy = new Dog();
Dog Doggi = new Dog();
Gunnar.setName("Gunnar");
Gunnar.setAge(20);
Gunnar.setHeight(215);
Sven.setName("Sven");
Sven.setAge(55);
Sven.setHeight(178);
Sven.sayHello(Gunnar);
}
}
Java wants me to change Sven and Gunnar (name of the two Person objects) to Person in the .setName lines so it would be Person.setName("Gunnar"); and Person.setName("Sven");. But, that won't work since there would be no way of telling which one is which. Please correct me if I am wrong. Also don't mind the dog code.
Here's the code for the Person class:
public class Person {
static int age;
static int height;
static String name;
public void sayHello(Person name) {
System.out.println("Hello!" +name);
}
public int getAge() {
return age;
}
public void setAge(int age) {
Person.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
Person.height = height;
}
public static String getName() {
return name;
}
public static void setName(String name) {
Person.name = name;
}
}

You are calling your methods correctly (from logical point of view)
but apparently you have declared them incorrectly (as static).
Make them instance methods by removing the static modifier.
That should fix your problem. And learn the the difference between
static and instance methods (seems you haven't quite yet).
In particular getName and setName should not be static.

Related

Why does my code result in a compile error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The code in question is as follows:
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.List;
public class Test{
public static void main(String[] args) {
List<Human> humans= newArrayList<Human>();
humans.add(newHuman(13));
humans.add(newHuman(33));
humans.add(newHuman(21));
humans.add(newHuman(21));
Collections.sort(humans);
System.out.print(humans.get(0).age);
System.out.print(humans.size());
}
}
class Human implements Comparable<Human> {
int age;
public Human(int age) {
this.age = age;
}
public int compareTo(Human h) {
return h.age.compareTo(this.age);
}
}
I was wondering why this code causes a compilation error? I'm not sure where I'm going wrong.
Some extra details:
Primitive type like shrot, int, long, double do not implement Comparable interface. make your instance variable Object type. also changes in imports, while creating new object ( new Human(13)).
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test{
public static void main(String[] args) {
List<Human> humans= new ArrayList<Human>();
humans.add(new Human(13));
humans.add(new Human(33));
humans.add(new Human(21));
humans.add(new Human(21));
Collections.sort(humans);
System.out.print(humans.get(0).age);
System.out.print(humans.size());
}
}
class Human implements Comparable<Human> {
Integer age;
public Human(int age) {
this.age = age;
}
public int compareTo(Human h) {
return h.age.compareTo(this.age);
}
}
you miss many spaces in code.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test{
public static void main(String[] args) {
List<Human> humans= new ArrayList<Human>();
humans.add(new Human(13));
humans.add(new Human(33));
humans.add(new Human(21));
humans.add(new Human(21));
Collections.sort(humans);
System.out.print(humans.get(0).age);
System.out.print(humans.size());
}
}
class Human implements Comparable<Human> {
int age;
public Human(int age) {
this.age = age;
}
public int compareTo(Human h) {
return h.age - this.age;
}
}

Avoiding main method static definition? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
The Person.setPhoneNumber is saying
Main method is not static in class PersonTest, please define the main method as:
public static void main(String[] args)
but I cannot define in Person class the string PersonName as static. What should I do then?
Person Class:
public class Person {
private String name;
private int age;
private String phoneNumber;
public Person(String n, int a){
name = n;
age = a;
phoneNumber = null;
}
public String getName(){
return name;
}
public void setname(String n){
this.name = n;
}
public int getAge(){
return age;
}
public void setAge(int a){
age = a;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setPhoneNumber(String pn){
this.phoneNumber = pn;
}
public String toString() {
return "Person {name=" + name +", age= " + age +", phone number =" + phoneNumber+ "}";
}
}
Person Test:
public class PersonTest {
public void main(String[] args){
Person person1 = new Person("Joel.Z", 20);
Person.setPhoneNumber("8324193601");
Person person2 = new Person("Fred Werd", 84);
Person.setPhoneNumber("585275333");
System.out.println(person1);
System.out.println(person2);
}
}
The main method should be static:
public static void main(String[] args)
Also, you should call you methods on an instance, not on a class:
public class PersonTest {
public static void main(String[] args){
Person person1 = new Person("Joel.Z", 20);
person1.setPhoneNumber("8324193601");
Person person2 = new Person("Fred Werd", 84);
person2.setPhoneNumber("585275333");
System.out.println(person1);
System.out.println(person2);
}
}
As #ΔλЛ says is correct, your main method must be static, also keep in mind that you must use the instance of the created object, it means:
Here you are creating person1 object:
Person person1 = new Person("Joel.Z", 20);
So, you must use that object to access the attribute:
person1.setPhoneNumber("8324193601");
instead of Person.setPhoneNumber("8324193601"); In the same way with the second object: person2.setPhoneNumber("585275333"); instead of Person.setPhoneNumber("585275333");

Why polymorphism doesn't play a role? [duplicate]

This question already has answers here:
why java polymorphism not work in my example
(3 answers)
Closed 6 years ago.
This output make me confused. Present Code First:
public class Animal{
int age;
public Animal(int age){
this.age = 0;
}
public int getAge(){
return age;
}
}
public class Wolf extends Animal{
int age;
Wolf(int age){
super(age);
this.age = age;
}
}
public class MainTest {
public static void main(String[] args){
Wolf wolfExample = new Wolf(2) ;
System.out.println("Age = " + wolfExample.getAge());
}
}
The output is:
0
My expected output is:
2
through debug this program it seemed that wolfExample.getAge() return the age of its parent not itself, why? base on polymorphism here should return the age of wolf, I will appreciate if you can give me some guide.
Note: Currently, I find inheritance is far difficult than what I think before.
Polymorphism exists for methods, not for fields. Therefore getAge(), which is only implemented in the Animal class, returns the member of the Animal class.
If you override getAge() in the Wolf class, i.e. add a
#Override
public int getAge() {
return age;
}
you'll get the value of the Wolf class member.
That said, it doesn't make sense to have an age member in both the base class and sub-class. If it's a property common to all Animals, it should only be in the Animal class.
So your Wolf class will become :
public class Wolf extends Animal {
Wolf(int age) {
super(age);
}
}
And your Animal constructor doesn't make sense. You should assign the passed age argument to the member of the class :
public Animal(int age) {
this.age = age;
}
You don't need "int age" in Wolf class. By commenting it out, you may get your desired output.
public class Animal{
int age;
public Animal(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
public class Wolf extends Animal{
//int age;
Wolf(int age){
super(age);
//this.age = age;
}
}

Is this a valid code for achieving abstraction in java using abstract classes and abstract methods [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
Is this a correct method to achieve abstraction? As this in the abstract class
public abstract class Employee {
private String name, address;
int basicSalary;
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public int getbasicSalary(){
return basicSalary;
}
public void setName(String name){
this.name= name;
}
public void setAddress(String address){
this.address= address;
}
public void setBasicSalary(int basicSalary){
this.basicSalary= basicSalary;
}
public abstract int getMonthlySalary();
}
This class extends the abstract employee class
class NormalEmployee extends Employee {
public NormalEmployee() {
// TODO Auto-generated constructor stub
}
public void setBasicSalary(int basicSalary) {
this.basicSalary=basicSalary;
};
// Method override for salarycalculation
#Override
public int getMonthlySalary() {
// TODO Auto-generated method stub
int monthlySalaryOfNormalEmployee= 1200;
System.out.println("Normal Employee Salary: "+monthlySalaryOfNormalEmployee);
return monthlySalaryOfNormalEmployee;
}
}
public class BonusEmployee extends NormalEmployee {
int bonusAmount;
This class extends normal employee class which is already inherits methods from employee class
public BonusEmployee() {
// TODO Auto-generated constructor stub
}
public int getBonus(){
return bonusAmount;
}
public static void main(String[] args) {
// Creating objects and calling methods
BonusEmployee bE= new BonusEmployee();
NormalEmployee nE= new NormalEmployee();
bE.setBonus(1200);
bE.setBasicSalary(1500);
bE.getMonthlySalary();
nE.getMonthlySalary();
}
public void setBonus(int bonusAmount){
this.bonusAmount=bonusAmount;
}
#Override
public int getMonthlySalary() {
int getMonthlySalary= basicSalary + getBonus();
System.out.println("Bonus Employee Salary: "+getMonthlySalary);
return basicSalary;
}
}
Since its giving me expected results so don't know if the implementation is right or not!
Abstraction in any programming language is more of a concept or approach for modeling aspects of the real world as software. Abstraction is taking something which is complex in nature and representing it in software in a way that is easy to understand or use, but still models or retains the essential elements of the concept in the real world.
To write some code and then ask 'have I achieved abstraction' would depend on what it is that you are attempting to represent in code.
There's a good definition of abstraction in software development in this article here: https://en.wikipedia.org/wiki/Abstraction#In_computer_science

Its saying that I have illegal characters but I don't know how to fix that [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 7 years ago.
Improve this question
public final class Hw7pr2
{
public static void main(String[] args)
{
System.out.println(“Starting …….”);
Scanner scan =new scanner(system.in);
log("Starting...");
Pet pet = new Pet();
work_like(pet);
pet = new Fish();
work_like(pet);
pet = new dog();
work_like(pet);
pet = new cat();
work_like(pet);
log("Done.");
}
public static void work_like(Pet aPet)
{ aPet.walk();
}
private static class Pet
{
Public:
Char name[10], color[10];
void prop()
{
log("pet is: ");
system.out.println(“enter Name and color of pet”);
name=scan.nextchar();
color =scan.nextchar();
}
}
private static final class Fish extends Pet
{
#Override void prop()
{
log("pet is fish...");
System.out.println(“Pet is fish”);
}
}
private static final class dog extends Pet
{
Char breed;
Int weight;
#Override void prop()
{
log("Pet is Dog ");
system.out.println(“Enter the breed”);
breed =scan.nextchar();
system.out.println(“Enter weight of the dog”);
weight=scan.nextchar();
}
}
private static final class cat extends Pet
{
Char coat;
#Override void prop()
{
log("Pet is cat");
system.out.println(“Enter Name and color of pet”);
coat =scan.nextchar();
}
}
private static void log(String aMessage)
{
System.out.println(aMessage);
}
}
It keeps saying that I have a lot of illegal characters but I don't know what that is so can someone help me?
In case you copied that from MS Word, this line looks wrong (dots):
System.out.println(“Starting …….”);

Categories

Resources