Instantiate error when I change class to abstract - java

I'm working on my intro to programming assignment. Previously I created a program that models an employee using classes for Address Name and Date. This week the assignment is adding subclasses for Hourly and Salaried employees. To start with I tried making my employee class abstract, but when I do that, I get an error in my ArrayList "Cannot instantiate the type Employee (I put in a comment that shows where this error is)" I have posted my code below-- If anyone could give me any suggestions I would really appreciate it I've been struggling with what to do for hours.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public abstract class Employee
{
private int id;
private Name name;
private Address address;
private Date date;
Employee (int id, Name name, Address address, Date date)
{
setId(id);
setName(name);
setAddress(address);
setDate(date);
}
//Setter
public void setId(int id)
{
this.id = id;
}
public void setName(Name name)
{
this.name = name;
}
public void setAddress(Address address)
{
this.address = address;
}
public void setDate(Date date)
{
this.date = date;
}
//Getter
public int getId()
{
return id;
}
public Name getName()
{
return name;
}
public Address getAddress()
{
return address;
}
public Date getDate()
{
return date;
}
public String toString()
{
return "ID: " +getId()+ "Name: " +getName()+ "Address: " +getAddress()+ "Hire Date: "+ getDate();
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Ask user for number of employees; create array of appropriate size
System.out.println("Enter the number of employees: ");
int numEmployees = input.nextInt();
List<Employee> employees = new ArrayList<>();
// Read information on individual employees.
for (int i = 0; i < numEmployees; i++)
{
System.out.println("Enter the employee ID number: " );
int id = input.nextInt();
input.nextLine(); //without this the scanner skips
System.out.println("Enter the first name of the employee: " );
String firstName = input.nextLine();
System.out.println("Enter the last name of the employee: " );
String lastName = input.nextLine();
System.out.println("Enter the street address of the employee: " );
String street = input.nextLine();
System.out.println("Enter the city where the employee resides: " );
String city = input.nextLine();
System.out.println("Enter the state where the employee resides (two letter abbreviation): " );
String state = input.nextLine();
System.out.println("Enter the zip code of the employee: " );
String zip = input.nextLine();
System.out.println("Enter the month the employee was hired (1-12): " );
int month = input.nextInt();
System.out.println("Enter the day the employee was hired (1-31): " );
int day = input.nextInt();
System.out.println("Enter the year the employee was hired (1900-2020): " );
int year = input.nextInt();
input.nextLine(); //without this the scanner skips to last name
Name name = new Name(firstName, lastName);
Address address = new Address(street, city, state, zip);
Date date = new Date(month, day, year);
//this is where I get the error
Employee employee = new Employee(id, name, address, date);
employees.add(employee);
}
/**
* Print out information on all the employees
* Use Foreach loop to iterate through ArrayList
**/
for(Employee employee : employees)
{
System.out.print("ID:" + employee.getId() + " ");
System.out.print("Name:" + employee.getName().getFirstName() + " ");
System.out.println(employee.getName().getLastName());
System.out.print("Address:" + employee.getAddress().getStreet() + " ");
System.out.print(employee.getAddress().getCity() + " ");
System.out.print(employee.getAddress().getState() + " ");
System.out.println(employee.getAddress().getZip());
System.out.print("Hire Date: " + employee.getDate().getMonth() + "/");
System.out.print(employee.getDate().getDay() + "/");
System.out.println(employee.getDate().getYear());
System.out.println();
}
input.close();
}
}

You cannot instantiate abstract classes in Java. You can, however, instantiate a quick non-abstract subclass from them. In this subclass you'd of course need to implement all methods that are abstract as well
abstract class Foo {
...
}
public static void main(String args[]) {
Foo foo = new Foo(); //Can't do
Foo foo = new Foo() {}; // this will work, as long as Foo has a null constructor; if Foo has abstract methods, make sure to define them concretely within the { ... } block
}

Usually abstract classes are used to provide the basic data/methods to subclasses.
You cannot instantiate an object of abstract class.*
It's just a level of program abstraction and a good practice to create a hierarchical class structure.
*But you may use a reference to abstract class for creating an object of a concrete type.
AbstractClass obj = new ConcreteClass(); // if ConcreteClass extends AbstractClass

Making a class abstract usually means that it will be used as a parent class for subclasses that need to implement the same methods. Abstract classes cannot be instantiated. Once you have created your required subclasses, HourlyEmployee and SalariedEmployee, you'll be able to define a new object like this:
Employee employee = new HourlyEmployee();
or
Employee employee = new SalariedEmployee();
Here's a great explanation regarding abstract classes: https://stackoverflow.com/a/1320887/6062407

Related

How can I solve with Java's map containsKey() method?

I checked the code and saving data to the HashMap is correct, when typing ADD. Then after choosing option FIND I can get to the dedicated function but the method is unable to show me found object even if it is correct 100%.
Please check this code out and tell me why it does not find right objects in "public void showInfo(String name, String secondName)" for class Company that is sparked by TYPING "FIND" in class CompanyApp
import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;
public class CompanyApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
options[] values = options.values();
int choose;
int EXIT_NR = 2;
Company ref = new Company();
do {
System.out.println("Available options: ");
for (options one : values) {
System.out.println(one.getDescription() + " - " + one.name());
}
System.out.println("Choose one: ");
try {
choose = options.valueOf(in.nextLine()).ordinal();
if (Objects.equals(EXIT_NR, choose)) break;
if (choose < 0 || choose >= options.values().length) {
throw new IllegalArgumentException("Choose 0, 1 or 2!");
}
options(choose);
} catch (InputMismatchException e) {
System.out.println("Choose a number ");
}
} while (1 == 1);
}
static void options(int choose){
Company ref = new Company();
Scanner info = new Scanner(System.in);
switch (choose){
case 0:
System.out.println("Type in the name of the worker: ");
String name = info.nextLine();
System.out.println("Type in the second name of the worker: ");
String secondName = info.nextLine();
System.out.println("Type in the salary: ");
double salary = info.nextDouble();
info.nextLine();
ref.add(new Employee(name, secondName, salary));
break;
case 1:
System.out.println("Type in the name of the worker you want to find: ");
String name2 = info.nextLine();
System.out.println("Type in the second name of the worker you want to
find: ");
String secondName2 = info.nextLine();
ref.showInfo(name2, secondName2);
break;
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Company {
private Map<String, Employee> map = new HashMap<>();
public void add(Employee employee){
String key = employee.getName() + " " + employee.getSecondName();
if(!map.containsKey(key)){
map.put(key, employee);
System.out.println("Added object to map");}
}
public void showInfo(String name, String secondName){
String key = name + " " + secondName;
System.out.println("in showinfo method");
if(map.containsKey(key)){
System.out.println("found an object");
Employee employee = map.get(key);
System.out.println(employee.getName());
}}}
enum options {
ADD("Add employee "), FIND("Find employee"), EXIT("Exit program");
private String description;
options(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Override
public String toString() {
return "options{" +
"description='" + description + '\'' +
'}';
}
}
String name;
String secondName;
double salary;
public Employee(String name, String secondName, double salary) {
this.name = name;
this.secondName = secondName;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
#Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", secondName='" + secondName + '\'' +
", salary=" + salary +
'}';
}
}
The problem is in the method static void options(int choose). You need to pass the Company-Object and use it there like this:
Call from main method (ref is the Company-Object you create in the main method)
options(choose, ref);
The options-method with the Company as second parameter:
static void options(int choose, Company ref){
Scanner info = new Scanner(System.in);
switch (choose){
case 0:
System.out.println("Type in the name of the worker: ");
String name = info.nextLine();
System.out.println("Type in the second name of the worker: ");
String secondName = info.nextLine();
System.out.println("Type in the salary: ");
double salary = info.nextDouble();
info.nextLine();
//use the passed Company here
ref.add(new Employee(name, secondName, salary));
break;
case 1:
System.out.println("Type in the name of the worker you want to find: ");
String name2 = info.nextLine();
System.out.println("Type in the second name of the worker you want to find: ");
String secondName2 = info.nextLine();
//and here
ref.showInfo(name2, secondName2);
break;
}
}
Explanation what is happening in your code
As mentioned, the problem is in the method static void options(int choose).
Here you create a new Company-Object which is not passed in any way to the main method.
This is what happens, when you use ADD and a FIND afterwards:
Call options from main method with ADD
new Company-Object is created in options
new Employee-Object is added to the Company from the previous point
the method ends -> the created Company-Object is "thrown away" (eligible for Garbage Collection)
Call options from main method with FIND
new Company-Object is created in options(therefore no Employees in it)
no Employee can be found, because there is no entry in the map of the newly created Company
The map is empty at the time when you're trying to get the data from it using FIND option. The reason for that is you recreate the Company object in the options method:
Company ref = new Company();
At the same time also the map is recreated so there are no records inside.
Also, the Company object in the main method is not used.

Errors when trying to call a method

I have been looking around online, and I am still unsure of how to call a method in my child class. I am trying to call the pay() method in Executive, and when I type in the following code into my if statement, I keep getting an error.
staff[3].awardBonus(bonus);
I keep getting an error with this method. I'm not sure how to call that method... Thanks for any help!`import java.util.Scanner;
import java.util.Scanner;
public class Tester
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
StaffMember[] staff = new StaffMember[4];
String internName = "Susan 2";
String empName = "Tyler O.";
String hrName = "Becky R.";
String execName = "Daniel H.";
String address = "Brighton";
String phone = "420 - 0000";
String SSN = "12345789";
double rate = 1000;
staff [0] = new Intern(internName, address, phone);
staff [1] = new Employee(empName, address, phone, SSN, rate);
staff [2] = new HourlyEmployee(hrName, address, phone, SSN, rate);
staff [3] = new Executive(execName, address, phone, SSN, rate);
for (StaffMember staffPrint : staff)
{
System.out.println (staffPrint.toString() + "\n");
}
System.out.println("If you would like to give an executive a bonus, press 1. \nIf you would like to increase the hours of an hourly employee, press 2.");
int input = scan.nextInt();
if(input == 1)
{
double bonus = 0;
System.out.println("Enter the bonus for your employee: ");
bonus = scan.nextDouble();
}
}
Here is the Executive class, Employee class and the StaffMember class
public class Executive extends Employee
{
public Executive(String name, String address, String phone, String SSN, double rate)
{
super(name, address, phone, SSN, rate);
}
public double pay()
{
double money = super.pay();
return money;
}
public String toString()
{
String employee = super.toString();
return employee;
}
public void awardBonus(double execBonus)
{
rate += execBonus;
}
}
Employee
public class Employee extends StaffMember
{
String SSN;
double rate;
public Employee(String name, String address, String phone, String SSN, double rate)
{
super(name, address, phone);
this.SSN = SSN;
this.rate = rate;
}
public double pay()
{
return rate;
}
public String toString()
{
String employee = "";
employee = ("Name: " + name + "\nAddress: " + address + "\nPhone Number: " + phone + "\nSocial Security Number: " + SSN + "\nPay: " + pay());
return employee;
}
}
StaffMember
public abstract class StaffMember
{
String name;
String address;
String phone;
public StaffMember(String name, String address, String phone)
{
this.name = name;
this.address = address;
this.phone = phone;
}
public String toString()
{
String employee = "";
employee = ("Name: " + name + "\nAddress: " + address + "\nPhone Number: " + phone);
return employee;
}
public abstract double pay();
}
staff is a StaffMember array. When you reference any item from it (as you do it staff[3]), you get a StaffMember.
StaffMember does not have a method awardBonus().
Your problem is that you are trying to call an undefined method on your StaffMember object. In fact the method awardBonus() was not defined in your StaffMember class.
And in the code staff[3].awardBonus(bonus) you were trying to call awardBonus() on staff[3] which is a StaffMember instance.
By providing an awardBonus for all StaffMembers, you can call it on a StaffMember. For instance:
public abstract class StaffMember
{
public void awardBonus(double bonus) {
if (bonus > 0) {
throw new IllegalStateException("Only executives receive a bonus");
}
}
...
public class Executive extends StaffMember
{
#Override
public void awardBonus(double bonus) {
P.S. be careful to inform the company on this.
You can use:
// Check if staff[3] is really an Executive
if(staff[3] instanceof Executive) {
// Cast staff[3] to an Executive
Executive executive = ((Executive)staff[3]);
// Now you can call awardBonus
executive.awardBonus(bonus);
}
Because staff is an StaffMember-array which doesn't contains the methode awardBonus.
You know that staff[3] is an Executive, but your program doesn't. Therefore you have to check if staff[3] is an instanceof Executive so you can safely cast it to an Executive with Executive executive = (Executive)staff[3].

How to create an arraylist using polymorphism?

I am trying to create an array list that contains all employees and is able to handle any type of employee. I also have to load the data onto to the list The class I'm using is called payroll. This is what I have so far:
The employee class looks like this:
import java.util.*;
public abstract class Employee
{
private String name, employeeNum, department;
private char type;
public Employee()
{
name ="";
employeeNum = "";
department = "";
}
public Employee(String Name, String EmpNum, String Depart)
{
name = Name;
employeeNum = EmpNum;
department = Depart;
}
//public EMpoy
public String getName()
{
return name;
}
public String getEmployeeNum()
{
return employeeNum;
}
public String getDepartment()
{
return department;
}
public char getType()
{
return type;
}
public void setName(String Name)
{
name = Name;
}
public void setEmployeeNum(String EmpNum)
{
employeeNum = EmpNum;
}
public void setDepartment(String Depart)
{
department = Depart;
}
public String toString()
{
String str;
str = "Employee Name: " + name + "\n"
+ "Employee Number: " + employeeNum + "\n"
+ "Employee Department: " + department + "\n";
return str;
}
}
The payroll class looks like this so far:
import java.util.*;
import java.io.*;
public class Payroll
{
private ArrayList<Employee> list = new ArrayList<Employee>();
private String fileName;
public Payroll()
{
}
public void fileName(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("InsertFileName");
String fileName1 = kb.next();
fileName = fileName1 + ".txt";
}
public void loadData() throws FileNotFoundException
{
Scanner s = new Scanner(new File(fileName));
while (s.hasNext())
{
String name = s.next();
String employeeNum = s.next();
String department = s.next();
//String typeString = s.next();
//char type = typeString.toUpperCase().charAt(0);
char type = s.next().toUpperCase().charAt(0);
if (type == 'S')
{
double yearlySalary = s.nextDouble();
list.add(new Salary (name, employeeNum, department, yearlySalary));
}
else if (type == 'H')
{
double hourlyPayRate = s.nextDouble();
String hours = s.next();
int hoursWorked = Integer.parseInt(hours);
list.add(new Hourly (name, employeeNum, department, hourlyPayRate, hoursWorked));
}
else if (type == 'C')
{
int numOfWeeks = s.nextInt();
double baseWeeklySalary = s.nextDouble();
int salesThisWeek = s.nextInt();
int salesThisYear = s.nextInt();
double commissionRate = s.nextDouble();
list.add(new Commission (name, employeeNum, department, numOfWeeks, baseWeeklySalary, salesThisWeek, salesThisYear, commissionRate));
}
}
s.close();
}
Now I know I'm supposed to make the arraylist in the constructor, that's what I'm having trouble with. How can I make the list using polymorphism to get every employee? Thanks.
Hi Srk93 You are getting error as your list contains the references of Employee class and Employee class does't have getCommissionRate method. You can call on Employee reference which are declared in Employee class. Create abstact method of calculateSalary() and implement in all your child classes.
Its duplicate of "cannot find symbol: method" but the method is declared

I want to add to an arrayList through user input

I have been trying to create a small program that prompts the user for input that takes an employee name and salary adds it to an arrayList, then displays options on the screen(e.g 0: quit, 1: add, 2: display), reads the input then proceeds based on the input.Displaying would just be(e.g Last Name: Smith Salary: £14000. Just need some help to point me in the right direction. I currently have 3 classes Employee, Employee List and Employee Test.
This class prompts the user input.
import java.util.Scanner;
public class Employee {
private String Last_Name;
private int Salary;
public Employee(){
Scanner inputValues = new Scanner(System.in);
System.out.print("Enter employee last name: ");
Last_Name = inputValues.next();
System.out.print("Enter Employee Salary: " + "£");
Salary = inputValues.nextInt();
}
public void Display(){
System.out.printf("Name: " + Last_Name + " " + "Salary: " + Salary);
System.out.println("\n");
}
}
This class is supposed to be adding the employees to an arraylist but im not sure if im doing it correctly.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeList extends Employee{
private List <Employee> employee = new ArrayList<Employee>();
public EmployeeList(Employee person) {
employee.add(person);
}
public void DisplayEmployees(){
System.out.println("Employee:" + employee.size());
displayList(employee);
}
public static void displayList(List employee) {
}
}
This is where the main method is
import java.util.Scanner;
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee();
employee.Display();
EmployeeList empList = new EmployeeList(employee);
empList.DisplayEmployees();
Scanner scanner = new Scanner(System.in);
System.out.println("0: quit, 1: add, 2: display");
String employees = scanner.next();
/* if (employees.equals("1")){
//not sure how to go back to displaying the user prompts
break;
} */
}
}
Few tips I can think of:
EmployeeList should not extend Employee. The main rule of OOP is that class A extends class B if B is a A. This is clearly not the case here - employeeList is not an employee, it's a list of employees (In my mind you don't need a class for this, just List<Employee>)
I'd separate the logic from the data. meaning - Employee class should only hold the employee's data, not deal with scanning and getting the input from the user. the constructor should be simple in my mind, something like:
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
The logic of getting the data should be outside of this class, either in an EmployeeHandler or in the main itself. Since you put it inside the employee, you are having troubles continuing when some of the logic is in the employee and some in the main.
the high-level code should be something like (I'll leave the details to you):
show the menu options to the user
if he wants to add user, get input for both variables, create the employee object and add it to the list
if he wants to display, go over the list and print (the printing can be done overriding toString in Employee class)
if he wants to quit, finish
continue this loop until he wants to quit
public class Employee {
private String Last_Name;
private int Salary;
public Employee(){
public String getLName(){
return Last_Name;
}
public void setLName(){
this.Last_Name = Last_Name;
}
public int getSalary(){
return salary;
}
public void setSalary(){
this.salary = salary;
}
}
}
then in your main method you can create the employee object.
public static void main(String[] args){
Employee employee = new Employee();
Scanner scanner = new Scanner(System.in);
employee.setLName = scanner.next();
employee.setSalary = scanner.nextInt();
}
If i were you I would just make an arraylist to hold all employees. I would prompt the input option for x amount of times and add to the end of the arraylist. the arraylist would be created as so
ArrayList<Employee> employeeList = new ArrayList<Employee>();
to add to it use add
employeeList.add(employee);
This should be able to get you started
EDIT:
OOPS, made several mistakes. edit with the following. Note that it is employee.setLastName(value) because the method setLastName is part of the employee class and it must be passed a value because we have defined that in the employee class.
Employee employee = new Employee();
Scanner scanner = new Scanner(System.in);
String tempName = scanner.next();
int tempSalary = scanner.nextInt();
employee.setLastName(tempName);
employee.setSalary(tempSalary);
EDIT 2:
try to print arraylists as follows. didnt test it. let me know how it works.
for (int i = 0; i< employeelist.size(); i++){
Employee temp = values.get(i);
System.out.println("Last Name: " + temp.getLname() + "Salary: " + temp.getSalary());
}
I modified my employee class to look like this:
public class Employee {
public String lastName;
private int salary;
public Employee(){
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
#Override
public String toString(){
return "Employee Last Name: " + lastName + "\n" + "Employee Salary: " + salary;
}
}
I have modified my EmployeeTest class:
This is my EmployeeTest class:
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeTest {
static ArrayList<Employee> employeeList = new ArrayList<Employee>();
public static void main(String[] args) {
for(int i=0;i<5;i++){
addEmployees(employeeList);
}
System.out.println(employeeList.toArray());
}
public static void addEmployees(ArrayList<Employee> employeeList){
Scanner scanner = new Scanner(System.in);
System.out.println("0: quit, 1: add, 2: display");
String options = scanner.next();
if(options.equals("1")){
System.out.print("Enter employee last name: ");
String lastname = scanner.next();
System.out.print("Enter Employee Salary: " + "£");
int salary = scanner.nextInt();
Employee employee = new Employee();
EmployeeList.add(employee);
}
else if(options.equals("2")){
for (Employee employee : employeeList){
/*System.out.println("Name: " + employee.getLastName() + ", " + "Salary: " + employee.getSalary());*/
System.out.println(employee);
}
}
else{
System.exit(0);
}
}
}
However when i press 2 as my options it doesn't display what is in my arraylist.
I had read that a toString method is used to get those details so i could print them to the screen. And that using this for loop gets each item in the list to display them. Am i missing something here?. Apologies for this dragging on a bit, im just wanting this to work.

Why are my codes error today and fine yesterday?

import java.util.Scanner;
public class CarTest {
/**
* #param args
*/
public static void main(String[] args) {
//create a Scanner object
Scanner input = new Scanner(System.in);
/**
*Creates a car! object from the Car class
*with the passing of these arguments
*year, make, model, price
*/
Car car1 = new Car("2008", "Nissan", "Pathfinder", "10,000");
//display toString using car1 object
System.out.println(car1.toString());
//user input for year
System.out.println("Please list your automobile for sale: " +
"\nPlease enter the year? ");
//String newYear variable is created for CarTest class
String newYear = input.nexLine();
//set year in car class for Car1 object to String newYear
car1.setYear(newYear);
//user input for make
System.out.println("Please enter the make? ");
//String newMake variable is created for CarTest class
String newMake = input.nextLine();
//set make in car class for Car1 object to String newMake
car1.setMake(newMake);
//user input for model
System.out.println("Please Enter the model?");
//String newModel variable is created for CarTest class
String newModel = input.nextLine();
//set model in car class for Car1 object to String newModel
car1.setModel(newModel);
//user input for price
System.out.println("How much would you sell your car for?");
//String newPrice variable is created for CarTest class
String newPrice = input.nextLine();
//set price in car class for Car1 object to String newPrice
car1.setPrice(newPrice);
//displays the new info to the screen
System.out.println(car1.toString());
}
}
from
public class Car {
//class variables are created
private String year;
private String make;
private String model;
private String price;
//default constructor
public Car() {
}
//constructor with arguments
public Car(String year, String make, String model, String price) {
this.year = year;
this.model = model;
this.make = make;
this.price = price;
}
//get make of the vehicle
public String getMake() {
return make;
}
//set make for vehicle
public void setMake(String make) {
this.make = make;
}
//get model of the vehicle
public String getModel(){
return model;
}
//set model for vehicle
public void setModel(String model) {
this.model = model;
}
//get price of the vehicle
public String getPrice() {
return price;
}
//set price for vehicle
public void setPrice(String price) {
this.price = price;
}
//get year of the vehicle
public String getYear() {
return year;
}
//set year for vehicle
public void setYear(String year) {
this.year = year;
}
public String toString() {
return "For Sale By Owner: " + year + " " + make + " " + model +
"\nSelling Price: $" + price + "\n ";
}
}
It worked fine when i handed it last week for an assignment, but today, when i came to my computer for a new homework, I found red x's on my car.java, and carTest.java.
My eclipse is saying that : from carTest.java is full of errors and:
error: main method not found in class homework.cartest. please define the main method
public static void main(String[] args)
error is in the way I defined CAR1:
Car car1 = new Car("2008", "Nissan", "Pathfinder", "10,000");
and the ways i am getting the inputs
String newYear = input.nexLine();
String newMake = input.nextLine();
String newModel = input.nextLine();
String newPrice = input.nextLine();
with the input.nextLine(); being underlined as an error in the code in eclipse.
also in the Car.java. the method String toSTring() causes an error stating "change toString() type to String type.
it worked fine when I handed it and got a good grade for it, but today I noticed my codes to be errors, when all was okay yesterday?
Is the file listed under package/project Homework?
If not, reading your error-message correctly, you're trying to run something that doesn't exist.
At the same time, as said above, you could be experiencing filepath issues. Creating a new project and copy-pasting your code (refactored copy) is usually the quickest solution.
Clean and build the project. May be an anomaly with your Java build path
Copied your project and runned it. Only thing that is wrong with it is:
String newYear = input.nexLine();
should be:
String newYear = input.nextLine();
For the rest, it works fine.
And in the toString method you should add an #override annotation
#Override
public String toString() {
return "For Sale By Owner: " + year + " " + make + " " + model
+ "\nSelling Price: $" + price + "\n ";
}
Also make sure your project is called CarTest and Source packages package cartest

Categories