LE 7.1
Create a separate class called Family. This class will have NO main().
Insert a default constructor in the Family class. This is a method that is empty and its header looks like this: public NameOfClass()
Except, for the main(), take all the other methods in LE 6.2 and put them in the Family class.
Transfer the class variables from LE 6.2 to Family.
Strip the word static from the class variables and the method headers in the Family class.
Cut and paste the main() from LE 6.2 into your program file for LE 7.1.Create an object of the family class.
Format: NameOfClass objectName = new NameOfClass();
Use that object for the method calls in the main().
Format: objectName.methodName()
Make sure both classes are in the same directory.
To run both programs, you must run the class with the main().
-----------------------------------------------
Here is LE62
import java.util.Scanner;
public class LE62
{
public static int arraySize(Scanner inp)
{
System.out.printf("How many family members in your immediate family
including yourself?");
return Integer.parseInt(inp.next());
}//End public static int arraySize
public static String[] setNames(int size, Scanner input)
{
String[] familyNames = new String[size];
for (int i = 0; i < size; i++)
{
System.out.printf("Enter family member %d : ", (i + 1));
familyNames[i] = input.next();
}//End for
return familyNames;
}//End public static String[] setNames
public static String[] setRoles(String[] familyNames, Scanner
input)
{
String[] familyRoles = new String[familyNames.length];
for (int i = 0; i < familyNames.length; i++)
{
System.out.printf("What relationship is %s to you? ",
familyNames[i]);
familyRoles[i] = input.next();
}//End For
return familyRoles;
}//End public static String[] setRoles
public static Integer[] setAges(String[] familyNames, Scanner
input)
{
Integer[] familyAges = new Integer[familyNames.length];
for (int i = 0; i < familyNames.length; i++)
{
System.out.printf("How old is %s? ", familyNames[i]);
familyAges[i] = Integer.parseInt(input.next());
}
return familyAges;
}//End public static Integer
public static void printFamilyInfo(String[] familyNames, String[]
familyRoles, Integer[] familyAges)
{
System.out.printf("%nMY FAMILY%n");
for (int i = 0; i < familyNames.length; i++)
{
System.out.printf("%nName: %s", familyNames[i]);
System.out.printf("%nRole: %s", familyRoles[i]);
System.out.printf("%nAge: %d%n", familyAges[i]);
}//End for
}//End public static void printFamilyInfo
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int size = arraySize(input);
String[] familyNames = setNames(size, input);
printFamilyInfo(familyNames, setRoles(familyNames, input),
setAges(familyNames, input));
System.exit(0);
}//End Main
}//End Class
-------Sample out put--------------
How many family members in your immediate family including yourself? 4
Enter family member 1: Fred
Enter family member 2: Wilma
Enter family member 3: Pebbles
Enter family member 4: Bambam
What relationship is Fred to you? Father
What relationship is Wilma to you? Mother
What relationship is Pebbles to you? Self
What relationship is Bambam to you? Brother
How old is Fred? 45
How old is Wilma? 43
How old is Pebbles? 18
How old is Bambam? 15
MY FAMILY
Name: Fred
Role: Father
Age: 45
Name: Wilma
Role: Mother
Age: 43
Name: Pebbles
Role: Self
Age: 18
Name: Bambam
Role: Brother
Age: 15
Family.java |
package le6.project; import java.util.Scanner; public class Family { private String[] familyNames; private String[] familyRoles; private Integer[] familyAges; public Family() { } public int arraySize(Scanner inp) { System.out.printf("How many family members in your immediate family including yourself ? "); return Integer.parseInt(inp.nextLine()); }//End public static int arraySize public String[] setNames(int size, Scanner input) { familyNames = new String[size]; for (int i = 0; i < size; i++) { System.out.printf("Enter family member %d : ", (i + 1)); familyNames[i] = input.nextLine(); }//End for return familyNames; }//End public static String[] setNames public String[] setRoles(String[] familyNames, Scanner input) { familyRoles = new String[familyNames.length]; for (int i = 0; i < familyNames.length; i++) { System.out.printf("What relationship is %s to you? ", familyNames[i]); familyRoles[i] = input.nextLine(); }//End For return familyRoles; }//End public static String[] setRoles public Integer[] setAges(String[] familyNames, Scanner input) { familyAges = new Integer[familyNames.length]; for (int i = 0; i < familyNames.length; i++) { System.out.printf("How old is %s? ", familyNames[i]); familyAges[i] = Integer.parseInt(input.nextLine()); } return familyAges; }//End public static Integer public void printFamilyInfo(String[] familyNames, String[] familyRoles, Integer[] familyAges) { System.out.printf("%nMY FAMILY%n"); for (int i = 0; i < familyNames.length; i++) { System.out.printf("%nName: %s", familyNames[i]); System.out.printf("%nRole: %s", familyRoles[i]); System.out.printf("%nAge: %d%n", familyAges[i]); }//End for }//End public static void printFamilyInfo public static void main(String[] args) { Scanner input = new Scanner(System.in); Family myFamily = new Family(); // creating Family object int size = myFamily.arraySize(input); String[] familyNames = myFamily.setNames(size, input); myFamily.printFamilyInfo(familyNames, myFamily.setRoles(familyNames, input), myFamily.setAges(familyNames, input)); System.exit(0); } } |
OUTPUT
LE 7.1 Create a separate class called Family. This class will have NO main(). Insert a...
Below is the code from LE 6.2 that LE 7.1 is referring to. import java.util.Scanner; public class lastNameLE62 { private static Scanner in = new Scanner(System.in); private static int size; public static void arraySize() { System.out.printf("How many sports are you interested in? "); size = Integer.parseInt(in.nextLine()); } public static String[] setFavoriteSports() { String []favSports = new String[size]; for(int i=1; i<=size; i++) { System.out.printf("Enter sport #%d: ", i); favSports[i-1] = in.nextLine(); } return favSports; } public static String[] setFavoriteTeams(String[] sports) {...
PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...
The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber secondumber static void mainsering Asks for two integer undan uiet", ..",ЛТеп shows the result according to Note Use the attached example program and add required methods. Then une a switch tructure in the try black to call proper method according to the input operator Sample Output 1 : 2074 Sample Output 2 D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMismatchException; import java.util.Scanner; public...
I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...
In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...
I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome to this Odd program!"); int userInput1 = input.nextInt(); } public...
Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...
using Data Structures using Java. Modify it to make the ADT resizable. For this ADT, the MAX_SIZE will not be final, but rather will be increased whenever an new item needs to be added and the array is full (ie. size==MAX_SIZE). Whenever the array needs to grow, add the lesser of: (i) half the current size, or; (ii) 50 additional. Have your ADT report its old and new maximum size whenever the resize operation is called by printing “ADT resized...
public class Pet { //Declaring instance variables private String name; private String species; private String parent; private String birthday; //Zero argumented constructor public Pet() { } //Parameterized constructor public Pet(String name, String species, String parent, String birthday) { this.name = name; this.species = species; this.parent = parent; this.birthday = birthday; } // getters and setters public String getName() { return name; ...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...