Java ObjectDB program: This project deals with the following classes for a lapto
ID: 3773142 • Letter: J
Question
Java ObjectDB program:
This project deals with the following classes for a laptop database: Product, Laptop, Processor, Memory, Company (Given below). Utility has generic functions to print collections and to extract the element from a singleton collection; this class will be compiled with the above five but will not be transformed into a persistent class.
For each of the following functions, you are to provide code of its body correctly implementing the specification given in the comment:
Processor.installedOn
Memory.installedOn
Company.memoryProcessor
Company.differentCompanyProcessor
Laptop.find
Laptop.HDandHardDrive
Laptop.speedPrice
Laptop.hasProcessor
Laptop.laptopProcessorMadeBySameCompany
Laptop.sameProcessor
Laptop.groupByCompany
All these functions are to use JDOQL queries. It may be advantageous to start with relevant JDOQL queries in set form. For visualization, it may be helpful to draw a UML diagram of the database. Do not change anything in the given class definitions and the function headers. Place all the classes in one folder, the default package.
Test1 is programmed to do some testing with example laptop1.odb database files, respectively. To test your function code by these, download them to the same folder that has your laptop class files, and run the main functions. The correct output should be identical to Test1 output. The contents of these databases can be inspected by Explorer. You might do your own testing by adding test code to the main function.
Explanation / Answer
package chegg;
import java.util.Collection;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
@javax.jdo.annotations.PersistenceCapable
public class Laptop extends Product
{
int price; // in dollars
boolean hasHDScreen; // has a high-definition screen?
int hardDriveCapacity; // in GB
Processor processor; // the preinstalled processor
Memory memory; // the preinstalled memory
Company madeBy; // the inverse of Company.makeLaptops
public Laptop(String mn, int p, boolean hd, int hdc)
{
modelName = mn;
price = p;
hasHDScreen = hd;
hardDriveCapacity = hdc;
}
public String toString()
{
return madeBy.name+" "+modelName+"; "+
processor.toString()+"; "+
memory.toString()+"; "+
"harddrive: "+hardDriveCapacity+" GB";
}
public static Laptop find(String mName, PersistenceManager pm)
/* Returns the laptop with the given model name "mName"; returns null if no such laptop exists.
The function is applied to the database held by the persistence manager pm. */
{
Query query = pm.newQuery(Laptop.class);
query.setFilter("this.modelName == mName");
query.declareParameters("String mName");
Collection result = (Collection) query.execute(mName);
if (result.size() == 1)
return Utility.extract(result);
else
return null;
}
public static Collection HDandHardDrive(int x, Query q)
/* Returns the collection of all laptops that have an HD screen and at least x GB of harddrive.
Sort the result by (hardDriveCapacity, modelName). */
{
q.setFilter("this.hasHDScreen && this.hardDriveCapacity >= x");
q.declareParameters("int x");
q.setOrdering("this.hardDriveCapacity ascending, this.modelName ascending");
return (Collection) q.execute(new Integer(x));
}
public static Collection speedPrice(float c, int p1, int p2, Query q)
/* Returns the collection of all laptops that have a processor clock speed of at least "c" GHz
and a price of at least "p1" and at most "p2" dollars.
Sort the result by (processor.clockSpeed, price, modelName). */
{
q.setFilter("this.processor.clockSpeed >= c && this.price >= p1 && this.price <= p2");
q.declareParameters("float c, int p1, int p2");
q.setOrdering("this.processor.clockSpeed ascending, this.modelName ascending");
return (Collection) q.execute(new Float(c), new Integer(p1), new Integer(p2));
}
public static Collection hasProcessor(String cName, Query q)
/* Returns the collection of all laptops that have processors made by
the company with the name "cName". Sort the result by (madeBy.name, modelName). */
{
q.setFilter("this.madeBy.name.equals("cName")");
q.declareParameters("String cName");
q.setOrdering("this.madeBy.name ascending, this.modelName ascending");
return (Collection) q.execute(cName);
}
public static Collection laptopProcessorMadeBySameCompany(Query q)
/* Returns the set of 3-tuples such that
laptop "lt" is preinstalled with processor "p" and company "c" makes both "lt" and "p".
Sort the result by (c.name, lt.modelName). */
{
}
public static Collection sameProcessor(Query q)
/* Returns the collection of all laptops each of which has at least one other laptop
preinstalled with the same processor. Sort the result by (madeBy.name, modelName). */
{
q.declareVariables("Laptop laptop1; Laptop laptop 2");
q.setFilter("laptop1.processor == laptop2.processor");
q.setOrdering("this.madeBy.name ascending, this.modelName ascending");
return (Collection) q.execute();
}
public static Collection groupByCompany(Query q)
/* Group the laptops by the companies that make them.
Then return the set of 4-tuples where:
num = the total number of laptops made by c
minSpeed = the minimum clock speed of the processors preinstalled on the laptops made by c
maxSize = the maximum memory size of the memories preinstalled on the laptops made by c
Sort the result by c.name. */
{
}
}
--------------------------------
package chegg;
import java.util.*;
import javax.jdo.*;
@javax.jdo.annotations.PersistenceCapable
public class Processor extends Product
{
float clockSpeed; // in gigahertz (GHz)
Company madeBy; // the inverse of Company.makeProcessors
public Processor(String mn, float cs)
{
modelName = mn;
clockSpeed = cs;
}
public String toString()
{
return madeBy.name+" "+modelName+" "+clockSpeed+" GHz";
}
public Collection installedOn(Query q)
/* Returns the collection of all laptops on which the target processor is preinstalled.
Represents the inverse of Laptop.processor. Sort the result by (madeBy.name, modelName). */
{
q.setFilter("this.processor == processor");
q.declareParameters("chegg.Processor processor");
q.setOrdering("this.madeBy.name ascending, this.modelName ascending");
return (Collection) q.execute(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Float.floatToIntBits(clockSpeed);
result = prime * result
+ ((madeBy == null) ? 0 : madeBy.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Processor other = (Processor) obj;
if (Float.floatToIntBits(clockSpeed) != Float
.floatToIntBits(other.clockSpeed))
return false;
if (madeBy == null) {
if (other.madeBy != null)
return false;
} else if (!madeBy.equals(other.madeBy))
return false;
return true;
}
}
--------------------------------
package chegg;
import java.util.*;
import javax.jdo.*;
@javax.jdo.annotations.PersistenceCapable
public class Memory extends Product
{
int size; // memory size in GB
public Memory(String mn, int s)
{
modelName = mn;
size = s;
}
public String toString()
{
return modelName+" "+size+" GB";
}
public Collection installedOn(Query q)
/* Returns the collection of all laptops on which the target memory is preinstalled.
Represents the inverse of Laptop.memory. Sort the result by (madeBy.name, modelName). */
{
q.setFilter("this.memory == memory");
q.declareParameters("chegg.Memory memory");
q.setOrdering("this.madeBy.name ascending, memory.modelName ascending");
return (Collection) q.execute(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + size;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Memory other = (Memory) obj;
if (size != other.size)
return false;
return true;
}
}
----------------------------------------------
package chegg;
import java.util.*;
import javax.jdo.*;
@javax.jdo.annotations.PersistenceCapable
public class Company
{
String name; // key
HashSet makeLaptops = new HashSet();
// The set of laptops this company makes
HashSet makeProcessors = new HashSet();
// The set of processors this company makes
public Company(String s)
{
name = s;
}
public String toString()
{
return name;
}
public static Collection memoryProcessor(float c, int s, Query q)
/* Returns the collection of all companies that make laptops that
have a processor clock speed of at least "c" GHz and a memory size of
at least "s" GB. Sort the result by name. */
{
q.declareVariables("Laptop laptop");
q.setFilter("this.makeLaptops.contains(laptop) && laptop.processor.clockSpeed >= c && laptop.memory.size >= s");
q.declareParameters("float c, int s");
q.setOrdering("this.name ascending");
return (Collection) q.execute(new Float(c), new Integer(s));
}
public static Collection differentCompanyProcessor(Query q)
/* Returns the collection of all companies that make at least two laptops
preinstalled with processors made by different companies. Sort the result by name. */
{
q.declareVariables("Laptop laptop1; Laptop laptop2");
q.setFilter("this.makeLaptops.contains(laptop1) && this.makeLaptops.contains(laptop2) && laptop1.processor.madeBy.name != laptop2.processor.madeBy.name");
q.setOrdering("this.name ascending");
return (Collection) q.execute();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((makeLaptops == null) ? 0 : makeLaptops.hashCode());
result = prime
* result
+ ((makeProcessors == null) ? 0 : makeProcessors.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Company other = (Company) obj;
if (makeLaptops == null) {
if (other.makeLaptops != null)
return false;
} else if (!makeLaptops.equals(other.makeLaptops))
return false;
if (makeProcessors == null) {
if (other.makeProcessors != null)
return false;
} else if (!makeProcessors.equals(other.makeProcessors))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.