0% found this document useful (0 votes)
22 views8 pages

Java Class Examples for Students and Books

The document contains three Java programming assignments. The first assignment involves creating a 'Student' class to manage student information, the second involves creating 'Author' and 'Book' classes to manage book details, and the third assignment performs various arithmetic operations and prints the results. Each assignment includes code snippets demonstrating the required functionality.

Uploaded by

as6600342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Java Class Examples for Students and Books

The document contains three Java programming assignments. The first assignment involves creating a 'Student' class to manage student information, the second involves creating 'Author' and 'Book' classes to manage book details, and the third assignment performs various arithmetic operations and prints the results. Each assignment includes code snippets demonstrating the required functionality.

Uploaded by

as6600342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 2:

[Link] a class 'Student' with three data members which are name, age and address.
The constructor of the class assigns default values name as "unknown", age as '0' and
address as "not available". It has two members with the name 'setInfo1' and 'setInfo2'
First method has two parameters for name and age and assigns the same whereas the second
method
takes has three parameters which are assigned to name, age and address respectively.
Print the name, age and address of 10 students.
CODE:
public class Student {
String name;
int age;
String address;

public Student(){
[Link] = "unknown";
[Link] = 0;
[Link] = "not available";
}
public void setInfo1(String name, int age){
[Link] = name;
[Link] = age;
}
public void setInfo2(String name, int age, String address){
[Link] = name;
[Link] = age;
[Link] = address;
}
public void printInfo(){
[Link]("Name: " + [Link] + ", Age: " + [Link] + ", Address: " +
[Link]);
}
public static void main(String args[]){
Student[] students = new Student[10];

students[0] = new Student();

students[1] = new Student();


students[1].setInfo1("Amit", 18);

students[2] = new Student();


students[2].setInfo2("Navin", 20, "123 Main St");

students[3] = new Student();


students[3].setInfo1("Choubey", 19);
students[3].setInfo2("Choubey", 19, "456 Oak Ave");

students[4] = new Student();


students[4].name = "Ankita";
students[4].age = 19;
students[4].address = "789 Pine Ln";

students[5] = new Student();


students[5].setInfo1("Anuradha", 23);

students[6] = new Student();


students[6].setInfo2("Aabid", 20, "101 Elm Rd");

students[7] = new Student();


students[7].setInfo1("Ayushi", 24);
students[8] = new Student();
students[8].setInfo2("Abhishek", 21, "202 Maple Dr");

students[9] = new Student();


students[9].setInfo1("Giri", 20);

[Link]("--- Student Information ---");


for (int i = 0; i < [Link]; i++) {
[Link]("Student " + (i + 1) + ": ");
students[i].printInfo();
}
}
}
OUTPUT:

2. Write a Java class Author with following features:


• Instance variables :
o firstName for the author’s first name of type String.
o lastName for the author’s last name of type String.
• Constructor:
o public Author (String firstName, String lastName): A constructor with
parameters, it creates the Author object by setting the two fields to the passed
values.
• Instance methods:
o public void setFirstName (String firstName): Used to set the first name of
author.
o public void setLastName (String lastName): Used to set the last name of
author.
o public double getFirstName(): This method returns the first name of the
author.
o public double getLastName(): This method returns the last name of the
author.
o public String toString(): This method printed out author’s name to the screen
Write a Java class Book with following features:
• Instance variables :
o title for the title of book of type String.
o author for the author’s name of type String.
o price for the book price of type double.
• Constructor:
o public Book (String title, Author name, double price): A constructor with
parameters, it creates the Author object by setting the the fields to the passed
values.
• Instance methods:
o public void setTitle(String title): Used to set the title of book.
o public void setAuthor(String author): Used to set the name of author of
book.
o public void setPrice(double price): Used to set the price of book.
o public double getTitle(): This method returns the title of book.
o public double getAuthor(): This method returns the author’s name of book.
o public String toString(): This method printed out book’s details to the screen
Write a separate class BookDemo with a main() method creates a Book titled “Developing
Java Software” with authors Russel Winderand price 79.75. Prints the Book’s string
representation to standard output (using [Link]).
CODE:
class Author {
private String firstName;
private String lastName;

public Author(String firstName, String lastName) {


[Link] = firstName;
[Link] = lastName;
}

public void setFirstName(String firstName) {


[Link] = firstName;
}

public void setLastName(String lastName) {


[Link] = lastName;
}

public String getFirstName() {


return firstName;
}

public String getLastName() {


return lastName;
}

public String toString() {


return firstName + " " + lastName;
}
}
class Book {
private String title;
private Author author;
private double price;

public Book(String title, Author author, double price) {


[Link] = title;
[Link] = author;
[Link] = price;
}

public void setTitle(String title) {


[Link] = title;
}

public void setAuthor(Author author) {


[Link] = author;
}

public void setPrice(double price) {


[Link] = price;
}

public String getTitle() {


return title;
}

public Author getAuthor() {


return author;
}
public double getPrice() {
return price;
}

public String toString() {


return "Title: " + title + "\nAuthor: " + author + "\nPrice: $" + price;
}
}

public class BookDemo {


public static void main(String[] args) {
Author author = new Author("Russel", "Winder");
Book book = new Book("Developing Java Software", author, 79.75);
[Link](book);
}
}
OUTPUT:

[Link] a Java program to print the result of the following operations.


Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
CODE:
public class ArithmeticOperations {
public static void main(String[] args) {
int resultA = -5 + 8 * 6;
int resultB = (55 + 9) % 9;
int resultC = 20 + -3 * 5 / 8;
int resultD = 5 + 15 / 3 * 2 - 8 % 3;

[Link]("a. -5 + 8 * 6 = " + resultA);


[Link]("b. (55 + 9) % 9 = " + resultB);
[Link]("c. 20 + -3 * 5 / 8 = " + resultC);
[Link]("a. 5 + 15 / 3 * 2 - 8 % 3 = " + resultD);
}
}
OUTPUT:

Common questions

Powered by AI

The main method acts as the entry point of the Java program and is responsible for creating instances of the classes to demonstrate their behavior. In the BookDemo class, the main method constructs an Author and a Book, and prints the Book's details, showcasing object interaction and class composition. Similarly, in the Student class example, the main method manages object creation and initialization, uses set methods to modify state, and invokes printInfo to display each student's details. This demonstrates encapsulation and polymorphism in action .

The Student class initializes data members with default values: "unknown" for the name, 0 for age, and "not available" for the address in its no-argument constructor. This ensures that a Student object always has a valid state. User-provided data can later be set using the setInfo1 and setInfo2 methods, enabling customization. This approach aids in providing a safety net by defaulting values and offers flexibility in updating the object's state when user input is available .

The Author and Book classes exhibit composition by having the Book class include an Author object as part of its state. This models a real-world relationship where a book naturally contains an author entity, reinforcing a strong ownership relationship. This approach is beneficial for software design because it improves modularity, allowing the Author class to be reused or extended separately from the Book class, and thereby simplifies maintenance and enhances code readability .

Using two methods, setInfo1 and setInfo2, offers flexibility, allowing users to update student data incrementally or fully, depending on available information. This approach aligns with encapsulation principles, providing controlled access to the object's state. However, it can complicate the class interface with slightly redundant methods and increase the risk of inconsistent object states if not used carefully. Consolidating both purposes into a single method could streamline the class design, reducing potential confusion .

To optimize the ArithmeticOperations class for clarity, explicit use of parentheses could emphasize the intended precedence, reducing potential misinterpretation. For instance, separating complex expressions into intermediary variables could also improve readability. Performance-wise, since integer arithmetic is already efficient in Java, improvements would focus more on code clarity. Refactoring to use descriptive variable names reflecting operations (e.g., resultSum, resultModulus) could enhance understandability .

The 'setInfo1' method in the Student class is used to set the name and age of a student, taking two parameters: a String for the name and an int for the age. The 'setInfo2' method extends this functionality by also allowing the setting of the address. It takes three parameters: a String for the name, an int for the age, and another String for the address. Thus, while both methods can set the name and age, only 'setInfo2' can additionally set the address .

The ArithmeticOperations program calculates: a) -5 + 8 * 6 yielding 43, as multiplication is performed before addition, so 8 * 6 = 48, then -5 + 48 = 43; b) (55 + 9) % 9 equals 1 because addition inside the parentheses gives 64, and 64 % 9 results in 1 as 9 fits into 64 seven times leaving a remainder of 1; c) 20 + -3 * 5 / 8 equals 19 due to precedence order, where the division of -15 / 8 truncates to 0, thus 20 + 0 = 20; d) 5 + 15 / 3 * 2 - 8 % 3 computes 13, with 15/3 = 5, 5*2 = 10, 8%3 = 2, so 5 + 10 - 2 = 13, all adhering to Java's precedence rules .

Separating Book and BookDemo into distinct classes helps in organizing the code more clearly, serving different purposes. Book represents the structure and behavior associated with book properties, while BookDemo acts as an execution point for demonstrating these behaviors. This separation allows for modular testing and reuse of the Book class in different contexts without altering the demo logic, making the program easier to maintain and extend .

The constructor in the Author class initializes an object by setting the firstName and lastName fields to the values provided as arguments. This ensures that every Author object is created with specific, non-null names. Such constructors are commonly used when an object must always have essential data upon creation, ensuring data integrity and reducing the likelihood of null references .

Returning a reference to an Author object from the getAuthor method, rather than just a string of the author's name, allows greater flexibility and functionality. This approach enables access to the Author object's full capabilities, such as modifying its attributes or comparing Author objects, thereby promoting encapsulation and object reusability. It also maintains the Author's identity as a distinct object, not merely text, supporting more complex operations and interactions .

You might also like