Question

In: Computer Science

java programing Q: Given the following class: public class Student { private String firstName; private String...

java programing

Q: Given the following class:

public class Student {

private String firstName;

private String lastName;

private int age;

private University university;

public Student(String firstName, String lastName, int age, University university) {

this.firstName = fisrtName;

this.lastName = lastName;

this.age = age;

this.university = university;

}

public String getFirstName(){

return firstName;

}

public String getLastName(){

return lastName;

}

public int getAge(){

return age;

}

public University getUniversity(){

return university;

}

public String toString() {

return "\nFirst name:" + firstName + "\nLast name:" + lastName + " \nAge:"+ age + "\nUniversity" + university;

}

}

a- Create the University class (data type). The class must include name, state, and the country as member variables; it must also include methods for returning and storing the name, state, and country.

b- Create client code to test the classes.

Solutions

Expert Solution

Code:

import java.util.*;

class University{
    private String name;
    private String state;
    private String country;


    public University(String name, String state, String country) {
        this.name = name;
        this.state = state;
        this.country = country;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "{" +
            " name='" + getName() + "'" +
            ", state='" + getState() + "'" +
            ", country='" + getCountry() + "'" +
            "}";
    }

}

class Student {
    private String firstName;
    private String lastName;
    private int age;
    private University university;

    public Student(String firstName, String lastName, int age, University university) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.university = university;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public University getUniversity() {
        return university;
    }

    public String toString() {
        return "\nFirst name:" + firstName + "\nLast name:" + lastName + " \nAge:" + age + "\nUniversity" + university;
    }
}

class Main {

    public static void main(String args[]) {
        University u1 = new University("University of California", "California", "USA");
        Student s1 = new Student("Javed","Akhtar",22,u1);

        System.out.println(s1.toString());
    }
}

Code output:

Code Screenshot:


Related Solutions

Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/...
Java programming. ** Create Account class include: 1/ private long   accountNumber; 2/ private String firstName; 3/ private String lastName; 4/ private double balance; 5/ public Account(long accountNumber, String firstName, String lastName, double balance); 6/ get/set methods for all attributes. 7/ public boolean deposit(double amount);   • deposit only if amount is greater than 10 but   less than 200, add the deposit   amount   to the   currentbalance. • if deposit is   successful return true,   otherwise return false; 8/ public boolean withdrawal(double amount); •...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true;...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT