I am in a programming class that has provided me with a project but I have no idea where to start and was hoping someone could push me in the right direction. I am only posting part of the project so that someone can show me a bit of the code to get an idea of how its done as I have taken a programming class before but I am out of practice.
Create an application called Registrar that has the following classes:
A Student class that minimally stores the following data fields for a student:
Name
Student id number
Number of credits
The following methods should also be provided:
A constructor that initializes the name and id fields
A method that returns the student name field
Methods to set and retrieve the total number of credits
I have removed most of the question as I am not trying to get the full answer but to just get this little sample to try to get going on the rest of the project.
I am also having trouble with the 2nd part as to how I can create names and ID's on a second program and retrieve them into the first program with the classes.
Here is a bit of an translation for what you need to do, the words in bold are keywords that, when googled, will most likely return information relevant to what you are doing.
A Student class that minimally stores the following data fields for a student:
This basically means to create a class which has the following properties:
• Name
• Student id number
• Number of credits
Think hard about what types of data those would be? What type do you need to create to store somebody's name? Or their Id? Remember, these are all properties
A constructor that initializes the name and id fields
Google constructor and learn all about how they work, pay special attention when a learning source discusses how to initialize properties inside of the constructor.
A method that returns the student name field
Research about methods and how you can create one to return your property Student Name. Learn how you will actually use this method.
Methods to set and retrieve the total number of credits
Research Getters and Setters and understand how they interact with a classes properties
Best of luck buddy, google is your best friend/lover in programming..
public class Student {
private String name;
private String id;
private int numOfCredits;
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getNumOfCredits() {
return numOfCredits;
}
public void setNumOfCredits(int numOfCredits) {
this.numOfCredits = numOfCredits;
}
}
Related
For this class,
class Person {
int age;
public Person(int age) {
this.age = age;
}
}
In my textbook, the author tells me that it's also OK to write age=age to replace this.age=age.
He says, if the parameter and the data member have the same name, the left side of the assignment symbol will be treated as the data member of the class, and the right side is the parameter.
I have jdk1.7 installed on my computer, and codes like age=age has no effect. In which version of Java does that work?
The book is dead wrong and should be replaced. Use a different book. age = age will never have any effect and hopefully produce a compiler warning.
He says, if the parameter and the data member have the same name, the left side of the assignment symbol will be treated as the data member of the class, and the right side is the parameter.
This is not valid in any version of Java released by Sun / Oracle, or any language that conforms to any release of the Java Language Specification.
Please provide the name and author of the book where you found this claim.
I have never seen this. At least Im sure Java 1.4 works the same as 1.7 in this respect.
Just stick to using:
this.age = age;
Perhaps the textbook refers to this case, as this will work. As there is no local variable age it will assign customAge to the class field age:
class Person {
int age;
public Person(int customAge) {
age = customAge;
}
}
I have a model class "Journey" in my project which has several methods to delete, create and list all of the journeys. I am using heroku and a postgresql database. I need to write a method that will return all journeys that have a similar address to one specified. I know the query structure would typically be something like SELECT address FROM Journey WHERE address ~~ arguement but I don't know what functions exist to do this in the play framework.
*public static void search(String address){
//query
//return matching journey results
}*
You need to use Model's Finder for an example:
package models;
import play.db.ebean.Model;
import javax.persistence.*;
#Entity
public class Journey extends Model {
#Id
public Integer id;
public static Finder<Integer, Journey> find
= new Model.Finder<>(Integer.class, Journey.class);
// other fields
public String address;
public String country;
}
so you can easily select records with:
List<Journey> allJourneys = Journey.find.all();
List<Journey> searchedJourneys = Journey.find.where().like("address", "%foo%").findList();
Journey firstJourney = Journey.find.byId(123);
In your base case you can add this to your model:
public static List<Journey> searchByAddress(String address){
return find.where().like("address", "%"+address+"%").findList();
}
Etc. It returns whole objects with relations, so in big data sets it can be too heavy, you can or even should also use more optimized queries with Finder's chained methods like select(), fetch() etc to point which data you need at the moment.
There are also other possibilities in Ebean's API, anyway you need to declare which approach is most optimal for you.
BTW, it's worthy to examine existing sample applications, for an example computer's database to get familiar with this ORM.
Edit
For case insesitive searching there are additional Expressions i.e. ilike (instead of like) , istartsWith, iendsWith, ieq, icontainsand iexampleLike. They does the same what version without i at the beginning.
You can preview them in the API as well.
I have a couple of domain classes which all inherit a BaseDomain class.
In this BaseDomain class I have a field that is public final String name;
The value of name is set in the constructor.
public class BaseDomain {
public final String name;
public BaseDomain() {
this.name = this.getClass().getCanonicalName();
}
}
This BaseDomain is extended by a few classes
public class Trip extends BaseDomain {
private int id;
public Trip(int id){
this.id = id;
}
}
So far so good.
I want to get the value of the field "name" in an object instance of Trip of a with the help of JXPath but can't. I can access the "id" field but not the "name" field.
JXPathContext jxPathContext = JXPathContext.newContext(trip);
jxPathContext.setLenient(true);
int id = (int)jxPathContext.getValue("/#id"); // This works.
String name = (String)jxPathContext.getValue("/#name"); // This does not work.
Is it possible to get the value of "name" with this setup and JXPath?
The code might have some syntax errors and/or other errors. I hope you all get the idea and understand my question.
Any help or pointer are welcome.
First of: I want to thank Kelly S. French for his quick replay.
It made me realize that I have to explain some more.
I want to use jxpath because I will eventually do a deeper search. For example: The Trip might hold a list of Locations which also extends the BaseDomain. Each Location can hold a list of PointOfInterest that extends BaseDomain.
Via reflection in other part of the code I want to be able to get a list of BaseDomain based on their type (class.getCanonicalName())
The object-tree is not based on xml, it is pure POJO.
As far as I have figured out, there is no way of writing a jxpath-query for finding a list of objects based on their type, class name and so on.
Is this correct?
Does some one know of a way to do that?
The easiest way out, even if it's ugly, is to have a field in the super class that holds the class-name. That is why I have done this ugly solution.
Eventually I want to create a jxpath-query that based on the trip returns an iterator of which ever object that is an instance of BaseDomain at any depth and not depending on which branch in the object tree the node is located, as long as I can get the class-name of the object I'm looking for.
Does any one know if it is possible to achive this with a jxpath-query?
Code example, links to blogs or other documentation is welcome and appreciated.
As before, I'm very grateful for any help.
if you have the trip instance, why can't you do this
string n = trip.name;
I can see that you have an XML representation of the trip, but using XPath would be for when you only have the XML for 'trip'.
If you still need to get name using XPath, post the XML that is generated. I would be willing to bet that the name attribute is not part of Trip, but part of the enclosing BaseDomain. If you are basing the jxPathContext on the Trip, you've already passed the nodes for the BaseDomain. You'd have to navigate back up somehow (like node.parent) or do that when you create the context,
// not sure about this approach
JXPathContext jxPathContext = JXPathContext.newContext(trip.super);
After looking at the JXPath manual on parent/child relationships why don't you try this:
String name = (String)jxPathContext.getValue("/Trip/../#name");
// or use "../#name", not sure it wouldn't need to be "/../#name"
I have two classes(Pojos) name Soldbookdetails.java and Bookdetails.java,
what I want to do is, I have to get data from Bookdetails table(Soldbookdetails.java) and save the same data to my Soldbookdetails table(Soldbookdetails.java).
ActionClass.java
private Double[] id;//With getter and setter
private Double[] quantity; //With getter and setter
Bookdetails book=new Bookdetails();//pojos,//With getter and setter
Soldbookdetails sbook=new Soldbookdetails();//pojos,//With getter and setter
BookdetailsDAO dao=new BookdetailsDAO();
SoldBooksTransactionDAO dao2=new SoldBooksTransactionDAO();
------------
(Note: my both pojo are same only their Class name are different)
My problem: I am unable to save record from Bookdetails.java to Soldbookdetails.java .(See My above ActionClass.java class,inside the execute method , i have mention the ERRROR).
After getting record by Bookid , i am unable to save the record into my Soldbookdetails.java.
Please help me to solve my problem.
Your saveSoldbooks(Soldbookdetails s) method takes an object of Soldbookdetails as argument but you are passing an object of class Bookdetails.
The thing you can do is you can add a method that copies the attributes of an Bookdetails object to an Soldbookdetails object (without the id field). Then you should try to save the object of Soldbookdetails using your existing method.
While keeping sold books and books in separated tables is (IMO) good perfomance issue, I think you don't need to create separated IDENTICAL classes for them.
Consider making you're class BookDetails an abstract and annotating it as MappedSuperclass:
#MappedSuperclass
public abstract class BookDetails{
// fields
// getters
// setters
}
#Entity
#Table(name="SoldBooks")
public class SoldBookDetails extends BookDetails{
// Currently blank
}
#Entity
#Table(name="BookDetails")
public class TradingBookDetails extends BookDetails{
// Currently blank
}
While currently you're classes are identical, at future you may decide to create others book types (like other table and classes for purchasing books to store or books in the way to customer) and, of course, there may appear different logic for SoldBooks and others, and in this case such 'abstraction' will simplify maintenance.
The only changes to you're saveSoldBook I currently see is to change it's argument type or provide type check in the body of the method, as long as while argument type is BookDetails, you can pass in the function any variable, derived from it - therefore, TraidingBookDetails, SoldBookDetails etc. (Changes needed depends on you're logic in this method and DAO itself).
I have a college management portal. I have to create the following classes for depicting the model.
class Address
{
String street;
String city;
}
class Contact
{
String phone;
String mobile;
}
abstract class Person
{
String name;
String age;
Address address;
Contact contact;
}
class Student extends Person
{
String course;
String stream;
String rollno;
}
class Faculty extends Person
{
String department;
String faculty id;
}
Now should i use the getter-setter methods for instance initialisation or constructors ?
What about the aggregation in class Person ??
How should the constructor work there ??
Yes I think you should have getter & setter methods for the model objects. Your attributes of the class should be made private.
The constructors are used to allow the mandatory information to be passed for object construction without which the object cannot be build (or the object is useless). Its like you cant create a person without name & age. In your case the address & contact many be optional. So you can set the address/contact information using mutator methods.
As Srinivas mentioned the constructors are not the alternatives for mutator method.
The purpose of constructor is make mandatory things available for object construction. Its like without raw material you cant build a building.
For this stuff these you need "beans" = private member variables with getters and setters for each. Depending on your needs you could create a constructor that has certain parameters to be set in the object at instantiation but also through the setters not directly (as certain setters might operate on the set value in a certain way so it is best that you do not mess up directly with stuff like this.student = "name").
For the faculty case I do not see why it extends Person and I do not think it should. As you say these are models they should reflect structures of tables in a database so at most each of them would extend an abstract class or implement a certain interface but not each other.
For the person aggregation also I believe that Contact And Address should be Integers as they represent a Foreign Key in another table and think about the case of many-to-many relation.
Models are first of all a reflection of the database in the application.