Java Exam 3 Short Answer
13 Questions
100 Views

Java Exam 3 Short Answer

Created by
@WellBlue

Questions and Answers

Write the constructor for Student class with three arguments (id, gpa, name).

public Student(int id, double gpa, String name) { this.id = id; this.gpa = gpa; this.name = name; }

Write the constructor for Student class with no arguments.

public Student() { this.id = 1000; this.gpa = 0.0; this.name = "No name yet"; }

Write the toString() method for the Student class.

@Override public String toString() { return "id=" + id + ", gpa=" + gpa + ", name='" + name + '"'; }

Write code to calculate the product of two strings representing large integers.

<p>String s3 = Double.toString(Double.parseDouble(s1) * Double.parseDouble(s2));</p> Signup and view all the answers

Write an expression that returns true if a character is an arithmetic operator.

<p>public static boolean isAnyCharIsArithmeticOperator(String input) { ... }</p> Signup and view all the answers

What attribute should be added to the Account class to track the number of accounts?

<p>private static int numberOfAccounts = 0;</p> Signup and view all the answers

Write a function to change the status of students to the next level.

<p>public void changeStatus(Student[] students) { ... }</p> Signup and view all the answers

Write the equals method for Student class.

<p>public boolean equals(Student other) { return this.id == other.id; }</p> Signup and view all the answers

Write a statement to throw an exception with a message.

<p>throw new Exception(&quot;Not enough sugar&quot;);</p> Signup and view all the answers

Declare a String type instance variable named id for the Employee class.

<p>String Id;</p> Signup and view all the answers

Write a method to check if a string is an email address.

<p>public boolean isEmailAddress(String s) { ... }</p> Signup and view all the answers

Write a method that returns the first character of each string in an ArrayList.

<p>public String f(ArrayList&lt;String&gt; list) { ... }</p> Signup and view all the answers

Define a class Telephone with a static method getFullNumber.

<p>public class Telephone { public static String getFullNumber(String number) { return &quot;785-&quot; + number; } }</p> Signup and view all the answers

Study Notes

Student Class Constructors

  • Constructor with parameters initializes id, gpa, and name attributes.
  • Default constructor initializes id to 1000, gpa to 0.0, and name to "No name yet".

Student Class Methods

  • toString() method returns a string representation of a student's details including id, gpa, and name.

String Multiplication

  • Code example multiplies two large number strings (s1 and s2) and assigns the result to s3 as a string.

Arithmetic Operator Check

  • Method checks if a character c is among the arithmetic operators: +, -, *, /, %, returning true if found.

Account Class

  • Introduces a static attribute numberOfAccounts to count instances of Account created.
  • Constructor updates the account count upon instantiation.

Student Status Update

  • Method iterates through an array of Student objects and updates their status from each level (FR → SO, SO → JR, JR → SR, SR → DONE).

Equals Method for Student

  • Method compares two Student objects based on their id to determine equality.

Exception Handling

  • Code demonstrates throwing a custom exception with the message "Not enough sugar".

Employee Class

  • Contains a public instance variable Id that is accessible by subclasses.
  • Method details() prints the employee ID to the console.

Email Address Validation

  • Method checks if a string contains exactly one @ sign, with no spaces, tabs, or newline characters.

String Construction from ArrayList

  • Method creates a string composed of the first characters of each string in an ArrayList.

Telephone Class Definition

  • Class includes a static method getFullNumber that prepends "785-" to a given phone number string without a constructor.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Test your knowledge on Java constructs with this quiz focused on the Student class. Questions cover key aspects like constructors with arguments and no arguments. Ideal for students preparing for Java exams.

More Quizzes Like This

Java Constructors Quiz
5 questions

Java Constructors Quiz

CleanestCrimson avatar
CleanestCrimson
Java Constructors Quiz
5 questions
Java Chapter 4: Constructors
29 questions
Costruttori di classe in Java
40 questions
Use Quizgecko on...
Browser
Browser