Why does my code result in a compile error? [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 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;
}
}

Related

Trying to extend one class to another [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
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;
}

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

Fuel consumption class java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
import java.util.Scanner;
public class LKM {
public static void main(String[] args){
Scanner keyboard=new Scanner(System.in);
String startKm=keyboard.nextLine();
String endKm=keyboard.nextLine();
String liters=keyboard.nextLine();
}
public void Car (double startOdo, double endOdo, double liters){
startKm=startOdo;
endKm=endOdo;
liters=liters;
}
public static void LKM(String args[]){
calculateLKM red=new Car(1,20,10);
Car white=new Car(5,10,5);
System.out.println((red.endKm-red.startKm)/red.liters);
System.out.println((white.endKm-white.startKm)/white.liters);
}
}
I have to define a class that calculates fuel consumption of a car using one constructors and one method. I tried learning class and objects but it didn't work so well..I need just a few tips. Thank you.
I think you are confusing Method and Constructor.
When you do "New Car()", you try to invoke Car Construstor.
So to do it, you need a class "Car" with a Constructor inside.
Create a new file named "Car.java" and insert this code inside :
public class Car {
public Car(double startOdo, double endOdo, double liters){
this.startOdo = startOdo;
this.endOdo = endOdo;
this.liters = liters;
}
}
But if you wan't to do :
startKm=startOdo;
endKm=endOdo;
liters=liters;
You need fields inside your Car Class. So add startKm, endOdo, liters as fields in your Car class :
private double startOdo;
private double endOdo;
private double liters;
Then add some getters and setters to access your fields :
public double getStartOdo() {
return startOdo;
}
public void setStartOdo(double startOdo) {
this.startOdo = startOdo;
}
public double getEndOdo() {
return endOdo;
}
public void setEndOdo(double endOdo) {
this.endOdo = endOdo;
}
public double getLiters() {
return liters;
}
public void setLiters(double liters) {
this.liters = liters;
}
This would give you something like this :
public class Car {
private double startOdo;
private double endOdo;
private double liters;
public double getStartOdo() {
return startOdo;
}
public void setStartOdo(double startOdo) {
this.startOdo = startOdo;
}
public double getEndOdo() {
return endOdo;
}
public void setEndOdo(double endOdo) {
this.endOdo = endOdo;
}
public double getLiters() {
return liters;
}
public void setLiters(double liters) {
this.liters = liters;
}
public Car(double startOdo, double endOdo, double liters){
this.startOdo=startOdo;
this.endOdo=endOdo;
this.liters=liters;
}
}
Finally, you will be able to do what you want in your main file. You just have to call "getStartOdo()", "getEndOdo()" and "getLiters()" to retrieve your datas and display it with System.out.println() method.
Oh, and don't forget to call your "LKM" method in your Main method, otherwise nothing will happen.

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

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