Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search Coderanch
Advance search
Google search
Register / Login
Nikolas Spendik
Greenhorn
+ Follow
2
Posts
2
Threads
0
Cows
since Oct 01, 2014
Merit badge:
grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
Ranch Hand Scavenger Hunt
Number Posts (2/100)
Number Threads Started (2/100)
Number Cows Received (0/5)
Number Likes Received (0/10)
Number Likes Granted (0/20)
Set bumper stickers in profile (0/3)
Report a post to the moderators (0/1)
Edit a wiki page (0/1)
Create a post with an image (0/2)
Greenhorn Scavenger Hunt
First Post
Number Posts (2/10)
Number Threads Started (2/10)
Number Likes Received (0/3)
Number Likes Granted (0/3)
Set bumper stickers in profile (0/1)
Set signature in profile
Search for a post/thread (0/3)
Set a watch on a thread
Save thread as a bookmark
Create a post with an image (0/1)
Recent posts by Nikolas Spendik
Help with searching though a linked list given and displaying it given an integer.
Hey, i need help with a part of my program where I need to display an object, which is in a linked list, given a vin number. It is the display_info method. Any help is appreciated:) I have put two of my classes below.
Here is what i have so far:
import java.util.*; public class Car implements Comparable<Car>, Cloneable { private String model; private String color; private int year; private int vin_number; private double price; public Car(){} public Car(String model, String color, int year, int vin_number, double price) { this.model = model; this.color = color; this.year = year; this.vin_number = vin_number; this.price = price; } public Object clone() { // shallow copy try { return super.clone(); } catch (CloneNotSupportedException e) { return null; } } public String getModel(){return model;} public String getColor(){return color;} public int getYear(){return year;} public int getVinNumber(){return vin_number;} public double getPrice(){return price;} public void setModel(String newModel){model = newModel;} public void setColor(String newColor){color = newColor;} public void setYear(int newYear){year = newYear;} public void setVinNumber(int newVinNumber){vin_number = newVinNumber;} public void setPrice(double newPrice){price = newPrice;} public String toString() { return "Model: " + model + " Color: " + color + " Year: " + year + " Vin Number: " + vin_number + " Price: " + price; } public int compareTo(Car car) { return car.getModel().compareTo(this.getModel()); //eg -1 //return this.getModel().compareTo(car.getModel()); //reversing gives 1 } public static void main(String [] args) { } }
import java.util.Scanner; import java.util.Collections; import java.util.LinkedList; import java.util.ListIterator; public class CarLot { public static void sortCars(LinkedList someList) { Collections.sort(someList); System.out.println(someList); } public static void displayInfo(LinkedList someList) { System.out.println(someList); Scanner input = new Scanner(System.in); System.out.println("Enter a vin number:"); int number = input.nextInt(); for(int i = 0; i < someList.size(); i++) { if(number.equals(someList)) { System.out.println(someList); break; } } } public static void addCar(LinkedList someList) { int numcars; Car mycar = new Car(); Scanner input = new Scanner(System.in); System.out.println("Enter a model: "); String model = input.nextLine(); System.out.println("Enter a color: "); String color = input.nextLine(); System.out.println("Enter a year: "); int year = input.nextInt(); System.out.println("Enter a vin number: "); int vin_number = input.nextInt(); System.out.println("Enter a price: "); double price = input.nextDouble(); someList.add(new Car(model, color, year, vin_number, price)); System.out.println(someList); } public static void displayValue(LinkedList someList) { int value = someList.size(); System.out.println("There are " + value + " cars in the lot "); System.out.println(); } public static void main(String [] args) { Car mycar = new Car("Toyota", "White", 2012, 1234, 130.25); Car mycar2 = new Car("Honda", "Black", 2013, 1235, 130.50); Car mycar3 = new Car("Toyota", "White", 2012, 1234, 130.25); Car mycar4 = new Car(); LinkedList myList = new LinkedList(); LinkedList<Car> myList2 = new LinkedList<Car>(); myList.add(mycar); System.out.println(myList); Car mycar5 = (Car) mycar2.clone(); System.out.println("Cloned Object: " + mycar5); addCar(myList2); } }
show more
9 years ago
Beginning Java
Help with linked lists
Hey guys,
I am having a little trouble with a part of my Java assignment that needs to have linked lists in it.
I need help to write a public method that takes an array of type object to load strings into a linked list.
Any help is appreciated:)
show more
9 years ago
Beginning Java