Contents There are many ways of doing this lab. Go for the solution closer to yo
ID: 3874831 • Letter: C
Question
Contents
There are many ways of doing this lab. Go for the solution closer to your Java skills. The most basic way is a series of “if” statements.
Set 1 task(s): Create three students using the "Set 1" data shown below. Prefix this output with two lines. On line one, output "Set 1". On line two, output twenty hyphens. Then, display all class variables of each student. After ensuring desired results are present, apply a single-line comment to introduce the logic you wrote as: // Set 1 task(s)...
Set 2 task(s): Create three more students using the "Set 2" data shown below. Prefix this output with two lines. On line one, output "Set 2". On line two, output twenty hyphens. Then, find and display the name of the youngest of these three students. After ensuring desired results are present, apply a single-line comment to introduce the logic you wrote as: // Set 2 task(s)...
Set 3 task(s): Create three more students using the "Set 3" data shown below. Prefix this output with two lines. On line one, output "Set 3". On line two, output twenty hyphens. Then, find and display the name of the oldest of these three students. After ensuring desired results are present, apply a single-line comment to introduce the logic you wrote as: // Set 3 task(s)...
By "find" above, I mean using the objects' data and doing calculations instead of you looking at the data and displaying John as the youngest and Fred as the oldest.
Test your solution with the following sets of data:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student.java;
public class app {
Student s1,s2,s3;
public app(Student st1, Student st2) {
s1 = st1;
s2 = st2;
}
public app(Student st1, Student st2, Student st3) {
s1 = st1;
s2 = st2;
s3 = st3;
}
public void findOldestOfTwo() {
if (s1.age>s2.age)
System.out.println("Oldest of two :" + s1.name + " Age:" + s1.age);
else
System.out.println("Oldest of two :" + s2.name + " Age:" + s2.age);
}
public void findOldestOfThree() {
if (s1.age>s2.age && s1.age>s3.age)
System.out.println("Oldest of three :" + s1.name + " Age:" + s1.age);
else if (s2.age>s1.age && s2.age>s3.age)
System.out.println("Oldest of three :" + s2.name + " Age:" + s2.age);
else
System.out.println("Oldest of three :" + s3.name + " Age:" + s3.age);
}
public void findYoungestofTwo() {
if (s1.age<s2.age)
System.out.println("Youngest of two :" + s1.name + " Age:" + s1.age);
else
System.out.println("Youngest of two :" + s2.name + " Age:" + s2.age);
}
public void findYoungestofThree() {
if (s1.age<s2.age && s1.age<s3.age)
System.out.println("Youngest of three :" + s1.name + " Age:" + s1.age);
else if (s2.age<s1.age && s2.age<s3.age)
System.out.println("Youngest of three :" + s2.name + " Age:" + s2.age);
else
System.out.println("Youngest of three :" + s3.name + " Age:" + s3.age);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1 = new Student("John Smith",20);
Student s2 = new Student("John Doe",21);
Student s3 = new Student("John Doe",44);
app app1 = new app(s1,s2);
app1.findYoungestofTwo();
app1.findOldestOfTwo();
app app2 = new app(s1,s2,s3);
app2.findYoungestofThree();
app2.findOldestOfThree();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package student.java;
public class Student {
int age;
String name;
public Student(String str, int i) {
age = i;
name = str;
}
}
dont know if i am doing it right and i dont know how to start last parts
Set 1 Set 2 Set 3 John Smith 20Zack Mills 21
Fred Fonz 44 John Smith 33
Zack Mills 22
Fred Fonz 20 John Smith 20
Zack Mills 40
Fred Fonz 30
Explanation / Answer
public class Student
{
int age;
String name;
public Student(String str, int i)
{
age = i;
name = str;
}
}
// another java file
public class set
{
public static void main(String[] args)
{
// for task 1 using set1 data
set task = new set();
task.set1();
System.out.println();
// for task 2 using set2 data
task.set2();
System.out.println();
// for task 3 using set3 data
task.set3();
}
// to calculate oldest of three students
public void findOldestOfThree(Student s1, Student s2, Student s3)
{
if (s1.age>s2.age && s1.age>s3.age)
System.out.println("Oldest of three :" + s1.name + " Age:" + s1.age);
else if (s2.age>s1.age && s2.age>s3.age)
System.out.println("Oldest of three :" + s2.name + " Age:" + s2.age);
else
System.out.println("Oldest of three :" + s3.name + " Age:" + s3.age);
}
// to calculate youngest of three students
public void findYoungestofThree(Student s1, Student s2, Student s3)
{
if (s1.age<s2.age && s1.age<s3.age)
System.out.println("Youngest of three :" + s1.name + " Age:" + s1.age);
else if (s2.age<s1.age && s2.age<s3.age)
System.out.println("Youngest of three :" + s2.name + " Age:" + s2.age);
else
System.out.println("Youngest of three :" + s3.name + " Age:" + s3.age);
}
public void set1()
{
Student set1_1 = new Student("John Smith",20);
Student set1_2 = new Student("Zack Mills",21);
Student set1_3 = new Student("Fred Fonz",44);
System.out.println("Set 1");
System.out.println("--------------------");
System.out.println(set1_1.name + " " + set1_1.age);
System.out.println(set1_2.name + " " + set1_2.age);
System.out.println(set1_3.name + " " + set1_3.age);
// simply accessing the variables of the class using dot operator
}
public void set2()
{
Student set2_1 = new Student("John Smith",33);
Student set2_2 = new Student("Zack Mills",22);
Student set2_3 = new Student("Fred Fonz",20);
System.out.println("Set 2");
System.out.println("--------------------");
findYoungestofThree(set2_1,set2_2,set2_3);
// using if condition to check whether the student's age is smaller than remaining two students
}
public void set3()
{
Student set3_1 = new Student("John Smith",20);
Student set3_2 = new Student("Zack Mills",40);
Student set3_3 = new Student("Fred Fonz",30);
System.out.println("Set 3");
System.out.println("--------------------");
findOldestOfThree(set3_1,set3_2,set3_3);
// using if condition to check whether the student's age is larger than remaining two students
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.