*LAB 3. //JOptionPane import javax.swing.JOptionPane; public class GetInputUsing
ID: 3746836 • Letter: #
Question
*LAB 3.
//JOptionPane
import javax.swing.JOptionPane;
public class GetInputUsingJOptionPane {
public static void main(String args[]) {
String name;
name = JOptionPane.showInputDialog("Name: ");
String age;
age = JOptionPane.showInputDialog("Age: ");
String address;
address = JOptionPane.showInputDialog("Address: ");
String number;
number = JOptionPane.showInputDialog("Contact Number: ");
String msg = "User's Personal Information: " +" "+" "
+"Name: "+ name +" "+"Age: "+ age +" "+"Address: "+ address +
" "+"Number: "+ number;
JOptionPane.showMessageDialog(null, msg);
}
}
********
//CircleCalculator
import java.util.Scanner;
public class GetInputUsingScanner {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int radius;
double pi = 3.14;
double area;
double diameter;
double circumference;
System.out.println("Enter radius: ");
radius = scan.nextInt();
System.out.println("Radius: " + radius);
System.out.println("Result:");
area = pi * (radius * radius);
System.out.println("Area: " + area);
diameter = 2 * radius;
System.out.println("Diameter: "+ diameter);
circumference = 2 * pi * radius;
System.out.println("Circumference: "+ circumference);
}
}
*LAB4
//ExamQuestions
import java.util.Scanner;
public class Exam {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] questions = {"Who is the first president of the Philippines ? ",
"Hitler party which came into power in 1933 is known as ?",
"For which of the following disciplines is Nobel Prize Award ?",
"What is 3*(2-1) ?",
"What is 1+1 ?"};
String[][] options = {{"1. Emilio Aguinaldo ","2. Joseph Estrada","3. Manuel Quezon ","4. GMA"},
{"1. Labour Party ","2. Nazi Party","3. Ku-Klux-Klan ","4. Democratic Party"},
{"1. Physics and Chemistry ","2. Physiology or Medicine","3. Literature, Peace and Economics ","4. All of the above"},
{"1. 1 ","2. 2","3. 3 ","4. 4"},
{"1. 1 ","2. 2","3. 3 ","4. 4"}};
int[] ans={1,2,4,3,2};
Scanner scan = new Scanner(System.in);
int correct=0;
for(int i=0;i<5;i++){
System.out.println(questions[i]);
for(int j=0;j<4;j+=2){
System.out.print(options[i][j]+" ");
System.out.printf(options[i][j+1]);
System.out.println();
}
System.out.print("Enter [1:4] : ");
int choose = scan.nextInt();
if(choose==ans[i]){
System.out.println("Correct! ");
correct+=1;
}
else{
System.out.print("Incorrect! The correct answer is: "+ options[i][ans[i]-1]+" ");
}
}
System.out.print("Congratulations, you got "+correct+" ");
System.out.print("That is a score of "+(float)((correct*100)/5));
System.out.println();
}
}
**************
//Factorial
import java.util.Scanner;
public class Factorial {
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number: ");
int num = scanner.nextInt();
int factorial = fact(num);
System.out.println("The factorial of "+num+" is "+factorial);
}
static int fact(int n){
int output;
if(n==1){
return 1;
}
output = fact(n-1)*n;
return output;
}
}
******************
//NumbersToWords
import java.util.Scanner;
public class NumberWords {
public static final String[] units = { "", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve","Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen" };
public static final String[] tens = {
"", // 0
"", // 1
"Twenty", // 2
"Thirty", // 3
"Forty", // 4
"Fifty", // 5
"Sixty", // 6
"Seventy", // 7
"Eighty", // 8
"Ninety" // 9
};
public static String converter(final int n) {
if((n>0)&&(n<100001))
{
if (n < 20)
{
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + converter(n % 100);
}
if (n < 100000) {
return converter(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + converter(n % 1000);
}
}
else
{
return "Number is out of range";
}
return "";
}
public static void main(final String[] args) {
//declaring the number
int n;
System.out.println("Enter the number :");
//Reading the number from user
Scanner input = new Scanner(System.in);
n = input.nextInt();
if (n == 100000) {
System.out.println("One Hundred Thousand");
}
converter(n);
System.out.println(converter(n));
}
}
****************************************
//Pyramid
import java.util.Scanner;
public class HalfPyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int number;
do {
System.out.println("Enter number: ");
number = scan.nextInt();
if (number <=1 ){
System.out.println("Error!");
}
else {
for(int i = 1; i <= number; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}while(number <= 1);
}
}
*LAB5
//AddressBook
class AddressBook
{
private String name;
private String address;
private String mobile;
private String email;
AddressBook()
{
//Default constructor
name = "Nikko";
address = "Pampanga";
mobile = "09123456789";
email = "nikkobarcelo";
}
AddressBook(String str1, String str2, String str3, String str4)
{
//Parameterized constructor
name = str1;
address = str2;
mobile = str3;
email = str4;
}
//Getter and setter methods
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMobile()
{
return mobile;
}
public void setMobile(String mobile)
{
this.mobile = mobile;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public static void main(String args[])
{
//This object creation would call the default constructor
AddressBook myobj = new AddressBook();
System.out.println("Name: "+myobj.getName());
System.out.println("Address: "+myobj.getAddress());
System.out.println("Mobile number: "+myobj.getMobile());
System.out.println("Email address: "+myobj.getEmail());
System.out.println();
/*This object creation would call the parameterized
* constructor StudentData(int, String, int)*/
AddressBook myobj2 = new AddressBook("Tony","Caloocan","09987654321","tony");
System.out.println("Name: "+myobj2.getName());
System.out.println("Address: "+myobj2.getAddress());
System.out.println("Mobile number: "+myobj2.getMobile());
System.out.println("Email address: "+myobj2.getEmail());
System.out.println();
AddressBook myobj3 = new AddressBook("Erlie","Antique","09245655325","erlie");
System.out.println("Name: "+myobj3.getName());
System.out.println("Address: "+myobj3.getAddress());
System.out.println("Mobile number: "+myobj3.getMobile());
System.out.println("Email address: "+myobj3.getEmail());
}
}
Explanation / Answer
import java.math.BigInteger;
import javax.swing.JOptionPane;
public class GetInputUsingJOptionPane {
public static void main(String args[]) {
String name;
name = JOptionPane.showInputDialog("Name: ");
while(!validName(name)){
name = JOptionPane.showInputDialog("Name: ");
}
String age;
age = JOptionPane.showInputDialog("Age: ");
while(!validNumeric(age)){
age = JOptionPane.showInputDialog("Age: ");
}
String address;
address = JOptionPane.showInputDialog("Address: ");
String number;
number = JOptionPane.showInputDialog("Contact Number: ");
while(!validNumeric(number)){
number = JOptionPane.showInputDialog("Contact Number: ");
}
String msg = "User's Personal Information: " +" "+" "
+"Name: "+ name +" "+"Age: "+ age +" "+"Address: "+ address +
" "+"Number: "+ number;
JOptionPane.showMessageDialog(null, msg);
}
static boolean validNumeric(String val){
boolean f=false;
try{
new BigInteger(val);
f=true;
}catch (Exception e) {
}
return f;
}
static boolean validName(String val){
boolean f=false;
try{
if(val.matches(".*\d+.*")){
f=false;
}else{
f=true;
}
}catch (Exception e) {
}
return f;
}
}
//////////////////////////////////////////
import java.util.Scanner;
public class GetInputUsingScanner {
public static void main(String[] args) {
int radius=0;
double pi = 3.14;
double area;
double diameter;
double circumference;
boolean done=true;
while(done){
System.out.println("Enter radius: ");
try{
Scanner scan = new Scanner (System.in);
radius = scan.nextInt();
scan.close();
done=false;
}catch(Exception e){
done=true;
System.out.println("Invalid Input: ");
}
}
System.out.println("Radius: " + radius);
System.out.println("Result:");
area = pi * (radius * radius);
System.out.println("Area: " + area);
diameter = 2 * radius;
System.out.println("Diameter: "+ diameter);
circumference = 2 * pi * radius;
System.out.println("Circumference: "+ circumference);
}
}
////////////////////////////
import java.util.Scanner;
public class Exam {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] questions = {"Who is the first president of the Philippines ? ",
"Hitler party which came into power in 1933 is known as ?",
"For which of the following disciplines is Nobel Prize Award ?",
"What is 3*(2-1) ?",
"What is 1+1 ?"};
String[][] options = {{"1. Emilio Aguinaldo ","2. Joseph Estrada","3. Manuel Quezon ","4. GMA"},
{"1. Labour Party ","2. Nazi Party","3. Ku-Klux-Klan ","4. Democratic Party"},
{"1. Physics and Chemistry ","2. Physiology or Medicine","3. Literature, Peace and Economics ","4. All of the above"},
{"1. 1 ","2. 2","3. 3 ","4. 4"},
{"1. 1 ","2. 2","3. 3 ","4. 4"}};
int[] ans={1,2,4,3,2};
//Scanner scan = new Scanner(System.in);
int correct=0;
for(int i=0;i<5;i++){
System.out.println(questions[i]);
int choose = 0;
for(int j=0;j<4;j+=2){
System.out.print(options[i][j]+" ");
System.out.printf(options[i][j+1]);
System.out.println();
}
boolean done=true;
while(done){
System.out.print("Enter [1:4] : ");
try{
Scanner scan = new Scanner (System.in);
choose = scan.nextInt();
done=false;
}catch(Exception e){
done=true;
System.out.println("Invalid Input: ");
}
}
if(choose==ans[i]){
System.out.println("Correct! ");
correct+=1;
}
else{
System.out.print("Incorrect! The correct answer is: "+ options[i][ans[i]-1]+" ");
}
}
System.out.print("Congratulations, you got "+correct+" ");
System.out.print("That is a score of "+(float)((correct*100)/5));
System.out.println();
}
}
////////////////////////////////////////////////////
import java.util.Scanner;
public class Factorial {
public static void main(String [] args){
int num = 0;
boolean done=true;
while(done){
System.out.println("Enter number: ");
try{
Scanner scanner = new Scanner (System.in);
num = scanner.nextInt();
scanner.close();
done=false;
}catch(Exception e){
done=true;
System.out.println("Invalid input: ");
}
}
int factorial = fact(num);
System.out.println("The factorial of "+num+" is "+factorial);
}
static int fact(int n){
int output;
if(n==1){
return 1;
}
output = fact(n-1)*n;
return output;
}
}
////////////////////////////////////
import java.util.Scanner;
public class NumberWords {
public static final String[] units = { "", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve","Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen","Eighteen", "Nineteen" };
public static final String[] tens = {
"", // 0
"", // 1
"Twenty", // 2
"Thirty", // 3
"Forty", // 4
"Fifty", // 5
"Sixty", // 6
"Seventy", // 7
"Eighty", // 8
"Ninety" // 9
};
public static String converter(final int n) {
if((n>0)&&(n<100001))
{
if (n < 20)
{
return units[n];
}
if (n < 100) {
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
}
if (n < 1000) {
return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + converter(n % 100);
}
if (n < 100000) {
return converter(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + converter(n % 1000);
}
}
else
{
return "Number is out of range";
}
return "";
}
public static void main(final String[] args) {
//declaring the number
boolean done=true;
int n=0;
while(done){
System.out.println("Enter number: ");
try{
Scanner scanner = new Scanner (System.in);
//Reading the number from user
n = scanner.nextInt();
scanner.close();
done=false;
}catch(Exception e){
done=true;
System.out.println("Invalid input: ");
}
}
if (n == 100000) {
System.out.println("One Hundred Thousand");
}
converter(n);
System.out.println(converter(n));
}
}
////////////////////////////////
import java.util.Scanner;
public class HalfPyramid {
public static void main(String[] args) {
int number=0;
do {
boolean done=true;
while(done){
System.out.println("Enter number: ");
try{
Scanner scanner = new Scanner (System.in);
//Reading the number from user
number = scanner.nextInt();
scanner.close();
done=false;
}catch(Exception e){
done=true;
System.out.println("Invalid input: ");
}
}
if (number <=1 ){
System.out.println("Error!");
}
else {
for(int i = 1; i <= number; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}while(number <= 1);
}
}
///////////////////no need to change as you are not accepting anything from user
public class AddressBook
{
private String name;
private String address;
private String mobile;
private String email;
AddressBook()
{
//Default constructor
name = "Nikko";
address = "Pampanga";
mobile = "09123456789";
email = "nikkobarcelo";
}
AddressBook(String str1, String str2, String str3, String str4)
{
//Parameterized constructor
name = str1;
address = str2;
mobile = str3;
email = str4;
}
//Getter and setter methods
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getMobile()
{
return mobile;
}
public void setMobile(String mobile)
{
this.mobile = mobile;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public static void main(String args[])
{
//This object creation would call the default constructor
AddressBook myobj = new AddressBook();
System.out.println("Name: "+myobj.getName());
System.out.println("Address: "+myobj.getAddress());
System.out.println("Mobile number: "+myobj.getMobile());
System.out.println("Email address: "+myobj.getEmail());
System.out.println();
/*This object creation would call the parameterized
* constructor StudentData(int, String, int)*/
AddressBook myobj2 = new AddressBook("Tony","Caloocan","09987654321","tony");
System.out.println("Name: "+myobj2.getName());
System.out.println("Address: "+myobj2.getAddress());
System.out.println("Mobile number: "+myobj2.getMobile());
System.out.println("Email address: "+myobj2.getEmail());
System.out.println();
AddressBook myobj3 = new AddressBook("Erlie","Antique","09245655325","erlie");
System.out.println("Name: "+myobj3.getName());
System.out.println("Address: "+myobj3.getAddress());
System.out.println("Mobile number: "+myobj3.getMobile());
System.out.println("Email address: "+myobj3.getEmail());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.