Can Anybody write help me to write java code for this project please? Here is th
ID: 3775662 • Letter: C
Question
Can Anybody write help me to write java code for this project please?
Here is the file of bag of objects from lab 2.
public class Cheese
{
private String cheeseType;
private double cheesePrice;
/**
* Constructor
* @param newCheeseType The value to store in cheeseType.
* @param newCheesePrice The value to store in cheesePrice.
*/
public Cheese(String newCheeseType, double newCheesePrice)
{
cheeseType = newCheeseType;
cheesePrice = newCheesePrice;
}
/**
* The setCheeseType method stores a value in the cheeseType field.
* @param newCheeseType The value to store in cheeseType.
*/
public void setCheeseType(String newCheeseType)
{
cheeseType = newCheeseType;
}
public String getCheeseType()
{
return cheeseType;
}
/**
* The setCheesePrice method stores a value in the cheesePrice field.
* @param newCheesePrice The value to store in cheesePrice.
*/
public void setCheesePrice(double newCheesePrice)
{
cheesePrice = newCheesePrice;
}
/**
* The getCheesePrice method returns a Cheese objects price.
* @return The value in the cheesePrice field.
*/
public double getCheesePrice()
{
return cheesePrice;
}
//@Override
// public String toString()
// {
// return "Cheese Type:" + cheeseType + "Cheese Price:" + cheesePrice;
// }
}
Here is the file for node<e> from lab 5
public class Node<E>
{ // Start main class
private Node<E> link;
private E data;
/**
* @param initialData is set to data
* @param initialLink is set to link
*/
public Node (E initialData, Node<E> initialLink)
{
initialData = data;
initialLink = link;
}
/*
* getData method calls for value from LinkedBag
* @return data
*/
public E getData()
{
return data;
}
/*
* getLink method calls for value from LinkedBag
* @return the value stored in link
*/
public Node<E> getLink()
{
return link;
}
/*
* setLink method sets data
* @param initialLink
*/
public void setData(E initialData)
{
}
public void setLink(Node<E> initialLink)
{
}
} // End main class
And here is the last file from Lab 5 Linked bag
public class LinkedBag<E>
{ // Start class
private Node<E> head; // declare variables
private int numElements; // declare variables
public LinkedBag()
{
head = null;
numElements = 0;
}
/*
* @param getSize gets the numbers stored
* @return the values of numElements
*/
public int getSize()
{
return numElements;
}
/*
* Add values
* @return the value stored in numElements
*/
public void add(E element)
{
head = new Node<E>(element, head);
numElements++;
}
/*
* countOccurrences method returns value stored in numOccurrences field
* @param target
* @return value in numOccurrences field
*/
public int countOccurrences(E target)
{
Node<E> cursor = head;
int numOccurences = 0;
for(cursor = head; cursor != null; cursor = cursor.getLink())
{
if (cursor.getData() == target)
{
numOccurences++;
}
}
return numOccurences;
}
/*
* exists method returns the value stored in found
* @param target
* @return value stored in found
*/
public boolean exists(E target)
{
boolean found = false;
Node<E> cursor = head;
while(cursor != null && !found)
{
if (cursor.getData() == target)
{
found = true;
}
else
{
cursor = cursor.getLink();
}
}
return found;
}
/*
* remove method returns the value stored in found
* @param target
* @return value stored in found
*/
public boolean remove(String target) {
Node<E> cursor = head;
boolean found = false;
while (cursor!= null && !found)
{
if (cursor.getData().equals(target))
found = true;
else
cursor = cursor.getLink();
}
if (found) {
cursor.setData(head.getData());
head = head.getLink();
numElements++;
}
return found;
}
@Override
public String toString() { // Start method
String stringToReturn = "";
Node<E> cursor;
if (numElements == 0) {
stringToReturn = "empty";
}
else {
for (cursor = head; cursor != null; cursor = cursor.getLink()) {
stringToReturn = stringToReturn + cursor.getData();
if (cursor.getLink() != null) {
stringToReturn = stringToReturn + ", ";
}
}
} // End method
return stringToReturn;
}
}// End class
Explanation / Answer
CheeseDemo.java
import java.util.Scanner;
class Cheese {//class name
private String cheeseType;
private double cheesePrice;
/**
* Constructor
*
* @param newCheeseType The value to store in cheeseType.
* @param newCheesePrice The value to store in cheesePrice.
*/
public Cheese(String newCheeseType, double newCheesePrice) {//constructor with arguments
cheeseType = newCheeseType;
cheesePrice = newCheesePrice;
System.out.println("Cheese Type= :" + cheeseType);
System.out.println("Cheese price :" + cheesePrice);
}
/**
* The setCheeseType method stores a value in the cheeseType field.
*
* @param newCheeseType The value to store in cheeseType.
*/
//setter and getter values
public void setCheeseType(String newCheeseType) {
cheeseType = newCheeseType;
}
public String getCheeseType() {
return cheeseType;
}
/**
* The setCheesePrice method stores a value in the cheesePrice field.
*
* @param newCheesePrice The value to store in cheesePrice.
*/
public void setCheesePrice(double newCheesePrice) {
cheesePrice = newCheesePrice;
}
/**
* The getCheesePrice method returns a Cheese objects price.
*
* @return The value in the cheesePrice field.
*/
public double getCheesePrice() {
return cheesePrice;
}
//@Override
// public String toString()
// {
// return "Cheese Type:" + cheeseType + "Cheese Price:" + cheesePrice;
// }
}
public class CheeseDemo {//main class
public static void main(String[] args) {//main method
String cheeseType;
double cheesePrice;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your favorite Cheese Type: ");
cheeseType = keyboard.nextLine();
System.out.print("Enter the price of Cheese: ");
cheesePrice = keyboard.nextDouble();
Cheese ces = new Cheese(cheeseType,cheesePrice);//constructor invoking
System.out.println(ces.toString());//coverting object
}
}
output
Enter your favorite Cheese Type: provolin
Enter the price of Cheese: 5.99
Cheese Type= :provolin
Cheese price :5.99
Cheese@1f96302
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.