What arguments would I need to print these messages? I would like to print something along the lines of:
Number: 999 999
Message: Hello this is a test message
Name: Jack
Time: 05:10:30
I'm pretty new to programming with Java so i'm unsure do I need get / set methods, please explain anything that you can see wrong with my code that can be improved.
Message:
public class Message {
//Instance variables
private static String phoneNumber = "0";//Default 0
private static String phoneMessage = "0";//Default 0
private static String phoneTime = "0";//Default 0
private static String callerName = "0";//Default 0
//Constructor
public Message(String number, String message, String time, String name){
this.phoneNumber = number;
this.phoneMessage = message;
this.phoneTime = time;
this.callerName = name;
}
public void displayMessage(){
System.out.println("Number: " + this.phoneNumber);
System.out.println("Message: " + this.phoneMessage);
System.out.println("Name: " + this.callerName);
System.out.println("Time: " + this.phoneTime);
}
}
Phone:
//The phone must be able to Create, Store and delete messages
//Store the messages in an array as an object
import java.util.*;
public class Phone {
public static void newMessage(){
getNumber();
}
//-------------------------------Get methods-----------------------------------
//Get the number of the client
public static String getNumber(){
String number;
System.out.printf("Please enter your number:\t");
Scanner scNumber = new Scanner(System.in);
number = scNumber.nextLine();
return number;
}
//Get the message
public static String getMessage(){
String message;
System.out.printf("Please enter your message:\t");
Scanner scMessage = new Scanner(System.in);
message = scMessage.nextLine();
return message;
}
//Get the time (in this case it wont be real time)
public static String getTime(){
String time = "05:10:30";
return time;
}
//Get the Name
public static String getName(){
String name;
System.out.printf("Please enter your name:\t");
Scanner scName = new Scanner (System.in);
name = scName.nextLine();
return name;
}
//-----------------------------Displaying the message-----------------------------
//Print the message
public static void displayMessage(int msgArray[]){
System.out.printf("Number: %s\nName: %s\nMessage: %s\nTime: %s\n",
getNumber(),getName(),getMessage(),getTime());
}
public static void main (String[] args){
//Array of Message objects (in this case maximum of 5)
Message msgArray[] = new Message[2];
for (int count = 0; count<msgArray.length; ){
msgArray[count] = new Message(getNumber(),getMessage(),getTime(),getName());
count++;
}
}
}


Sign In
Create Account

Back to top










