Using Java: Problem 1: Declare a class called Student that has instance variable
ID: 3786614 • Letter: U
Question
Using Java:
Problem 1:
Declare a class called Student that has instance variables: String sName, String sMajor and int sYear (corresponding to the name, major and year of the student). The instance variables must be declared with private access modifier.
Define a constructor that has three input parameters as: public Student (String name, String major, int year) and sets the appropriate instance variables. Also define a method public void displayInfo () that displays the information of the Student (including the name, major and year).
Now declare a driver class called StudentTest that instantiates two student objects: student1 with name = Adam, major = CSC, year = 2; and student2 with name = Eve, major = CIS, year = 1.
The code for instantiating the Student objects will look like (fill in the blanks appropriately):
Student student1 = new Student (…);
Student student2 = new Student (…);
Now invoke the displayInfo() method in Student class for the two student objects to display the information of the two students. Sample output is shown below.
Sample output:
Student 1's info:
name = Adam
major = CSC
year = 2
Student 2's info:
name = Eve
major = CIS
year = 1
Problem 2:
Declare a class called Divisors that has a method called computeDivisors that computes all the divisors of the number other than 1 and itself (note: a divisor of a number divides that number), and displays them.
For instance, the divisors of 12 are: 2, 3, 4, 6
Now write a main method in the Divisors class that gets a number from user and calls computeDivisors method that displays the divisors as shown below.
Sample output is given below:
Enter an integer (we will display its divisors): 12
The divisors of 12 are:
2 3 4 6
Hint: to compute the divisors of a number (say n), loop from 2 … n/2. For each number in the loop (say i), if n % i == 0, then i is a divisor of n.
Problem 3:
Now add one more method to the Divisors class called numDivisors that returns the number of divisors of the number other than 1 and itself. For instance, if we call numDivisors(12), it should return 4 (as there are 4 divisors).
Now create another class called PrimeNumber. Create a main method in PrimeNumber class that creates an object of Divisors. Get a number to test for prime from the user, call the numDivisors method in Divisors class, and then display if the user number is prime or not (if numDivisors returns 0, the number is prime; otherwise it is not prime).
Sample output:
Enter an integer (we will check if it is prime): 12
12 is not prime.
Enter an integer (we will check if it is prime): 31
31 is prime.
Explanation / Answer
1)
Student.java
public class Student {
//Declaring the instance variables
private String sName;
private String sMajor;
private int sYear;
//Parameterized constructor
public Student(String sName, String sMajor, int sYear) {
super();
this.sName = sName;
this.sMajor = sMajor;
this.sYear = sYear;
}
//This method will display the contents of the Student class object
public void displayInfo ()
{
System.out.println("name="+sName);
System.out.println("major="+sMajor);
System.out.println("year="+sYear);
}
}
______________________
StudentTest.java
public class StudentTest {
public static void main(String[] args) {
//Creating the Student class objects by passing the parameters
Student s1=new Student("Adam","CSE",2);
Student s2=new Student("Eve","CIS",1);
//Displaying the 1st Student information
System.out.println("Student 1's info:");
s1.displayInfo();
//Displaying the 2nd Student information
System.out.println("Student 2's info:");
s2.displayInfo();
}
}
______________________
Output:
Student 1's info:
name=Adam
major=CSE
year=2
Student 2's info:
name=Eve
major=CIS
year=1
____________________
2)
Divisiors.java
import java.util.Scanner;
public class Divisiors {
//This method displays the divisors of the user entered number
public static void computeDivisors(int number)
{
System.out.println("The Divisors of "+number+" are :");
for(int i=2;i<number;i++)
{
if(number%i==0)
System.out.print(i+" ");
}
}
public static void main(String[] args) {
int number;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the number entered by the user
System.out.print("Enter an integer (we will display its divisors) :");
number=sc.nextInt();
//Calling the method by passing the user entered number as argument
computeDivisors(number);
}
}
_______________________
Output:
Enter the number :12
The Divisiors of 12 are :
2 3 4 6
_______________________
3)
Divisiors.java
import java.util.Scanner;
public class Divisiors {
static int no_of_divisiors;
//This method displays the divisors of the user entered number
public static void computeDivisors(int number)
{
System.out.println("The Divisors of "+number+" are :");
for(int i=2;i<number;i++)
{
if(number%i==0)
{
System.out.print(i+" ");
}
}
}
public static int numDivisors(int number)
{
int count=0;
System.out.println("The Divisors of "+number+" are :");
for(int i=2;i<number;i++)
{
if(number%i==0)
{
count++;
}
}
return count;
}
public static void main(String[] args) {
int number;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the number entered by the user
System.out.print("Enter an integer (we will display its divisors) :");
number=sc.nextInt();
//Calling the method by passing the user entered number as argument
computeDivisors(number);
//Calling the method by passing the user entered number as argument
int no_of_divisiors=numDivisors(number);
System.out.println("The Number of divisiors of "+number+" are "+no_of_divisiors);
}
}
______________________
Output:
Enter an integer (we will display its divisors) :12
The Divisors of 12 are :
2 3 4 6 The Divisors of 12 are :
The Number of divisiors of 12 are 4
______________________
Divisors.java
import java.util.Scanner;
public class Divisors {
static int no_of_divisiors;
//This method displays the divisors of the user entered number
public static void computeDivisors(int number)
{
System.out.println("The Divisors of "+number+" are :");
for(int i=2;i<number;i++)
{
if(number%i==0)
{
System.out.print(i+" ");
}
}
}
public static int numDivisors(int number)
{
int count=0;
System.out.println("The Divisors of "+number+" are :");
for(int i=2;i<number;i++)
{
if(number%i==0)
{
count++;
}
}
return count;
}
public static int isPrime(int number) {
if(number<2)
return 0;
// If the user entered number is '2' return true
else if(number == 2)
return 1;
for (int i = 2; i * i <= number; i++) {
if (number % i == 0)
return 0;
}
return 1;
}
public static void main(String[] args) {
int number;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the number entered by the user
System.out.print("Enter an integer (we will display its divisors) :");
number=sc.nextInt();
//Calling the method by passing the user entered number as argument
computeDivisors(number);
//Calling the method by passing the user entered number as argument
int no_of_divisiors=numDivisors(number);
System.out.println("The Number of divisiors of "+number+" are "+no_of_divisiors);
}
}
_____________________
PrimeNumber .java
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
int number;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
Divisors div=new Divisors();
System.out.print("Enter an integer (we will check if it is prime): :");
number=sc.nextInt();
int res=div.isPrime(number);
if(res==1)
System.out.println(number+" is not prime.");
else if(res==0)
System.out.println(number+" is prime.");
}
}
__________________
Output:
Enter an integer (we will check if it is prime): :12
12 is prime.
__________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.