Trying to extend one class to another [closed] - java

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
Currently I have a working class called Humane32 and I want to extend my class Fysikere42 with this one, I have 2 arguments in Human, age and name, and one additional on the Fysikere42. The Humane32 class is currently looking like this:
import java.util.List;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
public class Humane32{
public static class Human{
private static final List<String> names = Arrays.asList("Rutger","Oscar","Aram","Noak","Hilda","Dahl");
public Random rand=new Random();
public String name;
public int age;
public Human(int age, String name){
this.age=age;
this.name=name;
}
public Human(){
this.age=rand.nextInt(101);
this.name=names.get(rand.nextInt(names.size()));
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public String toString(){
return "åldern är:"+this.age+"\n"+"namnet är:"+this.name;
}
}
public static void main(String[] args){
ArrayList<Human> humaner=new ArrayList<Human>();
int q;
for (q=0;q<=15;q++){
humaner.add(new Human());
System.out.println(humaner.get(q).toString());
}
}
}
And my new class is currently looking like this:
import java.util.List;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
public class Fysikere42 extends Humane32{
public static class Fysikerere42{
public int year;
public Fysikere42(int age, String name,int year){
this.age=age;
this.name=name;
this.year=year;
}
public int getYear(){
return this.year;
}
}
public static void main(String[] args){
System.out.println("hej");
}
}
But I'm getting the error message, "invalid method declaration, return type required". Where did it go wrong?

You're defining a constructor for Fysikere42 inside the static inner class Fysikerere42 since the names do not match it does not read it as a constructor for Fysikerere42 and hence it is asking for a return type for this method. (Since constructor methods don't have return types but it doesnt think this is a constructor).
Change the name of the constructor to Fysikerere42:
public class Fysikere42 extends Humane32{
public static class Fysikerere42{
public int year;
public Fysikerere42(int age, String name,int year){
this.age=age;
this.name=name;
this.year=year;
}

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");

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

Java and printf [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
The code is having some problem with class 2 line 12. Kindly help .
The code is in the image.
import java.util.Scanner;
public class first{
public static void main(String[] args){
Scanner b = new Scanner(System.in);
pro nc = new pro();
System.out.println("Enter the name of your first true crush");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
public class pro{
private String gname;
public String getName() {
return gname;
}
public void setName(String name) {
this.gname = name;
}
public void etc(){
System.out.printf("The name of your true crush was %s",getName());
}
}
In class 2 you have not specified the location of the function in System.out.printf(). You should use the this keyword and re-write the function as
public class pro {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void etc(){
System.out.printf("The carp is %s",this.getName());
}
}
there is no problem with this
I AM USING NETBEANS IDE.
/*
///
public class pro {
private String gname;
public String getname() {
return gname;
}
public void setname(String name) {
gname = name;
}
public void etc(){
System.out.printf("Tht name is %s",getname());
}
}
/////////
import java.util.Scanner;
public class first {
public static void main(String[] args) {
Scanner b= new Scanner(System.in);
pro nc = new pro();
System.out.println("enter the name");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
/////////
*/
Recheck this.
I have not changed your code, just typed it.
And yes I agree with Arc676 that people should post the code.
There is no problem on netbeans.
You can try to use System.out.format instead of System.out.printf.
import java.util.Scanner;
public class first{
public static void main(String[] args){
Scanner b = new Scanner(System.in);
pro nc = new pro();
System.out.println("Enter the name of your first true crush");
String temp = b.nextLine();
nc.setname(temp);
nc.etc();
}
}
public static class pro{
private String gname;
public String getName() {
return gname;
}
public void setName(String name) {
this.gname = name;
}
public void etc(){
System.out.format("The name of your true crush was %s", getName());
}
}

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

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.

Categories

Resources