Question

Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...

Java Homework Problems:

4.

• Examine AddImport.java.

– Perform the following:

– Replace the fully qualified name to access the Jlabel

component with an import statement.

– To import classes from the util package, replace multiple

import statements with a single import statement.

/*

* 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.

*/

import java.util.Calendar;

import java.util.Date;

public class AddImport {

    public static void main(String args[]) {

        javax.swing.JLabel label = new javax.swing.JLabel("hello");

    }

}

===========================================================

5.

Exercise 1

• Examine ShoppingCart.java.

– Perform the following:

– Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx.

– Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName.

public class ShoppingCart {

    public static void main (String[] args){

        String custName = "Steve Smith";

        String firstName;

        int spaceIdx;

       

        // Get the index of the space character (" ") in custName.

        // Use the substring method parse out the first name and print it.

    }

   

}

Exercise 2

Examine NameMaker.java.

• Perform the following:

– Declare String variables: firstName, middleName, lastName, and fullName

– Prompt users to enter their first, middle, and last names and read the names from the keyboard.

– Set and display the fullName as firstName+a blank char+middleName+a blank char+lastName.

public class NameMaker {

   

    public static void main(String args[])

    {

       

    }

   

}

Exercise 2

• Which do you think is preferable for this scenario?

• That is, the string concatenation operator or the concat() method?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

AddImport.java

//To import classes from the util package, replace multiple
//import statements with a single import statement.
import java.util.*;
//import all swing package
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class AddImport {

public static void main(String args[]) {
   //JFrame
   JFrame f=new JFrame();
  
   //JLabel
   JLabel label = new JLabel("hello");
  
   //JPanel
   JPanel p = new JPanel();
  
   //add JLabel To Panel
   p.add(label);
  
   //add JPanel to JFrame
   f.add(p);
  
// set the size of frame
f.setSize(300, 300);
  
//To show JFrame setVisible(true)
f.setVisible(true);

}

}

Output

5. Exercise 1

ShoppingCart.java

public class ShoppingCart {

public static void main (String[] args){

   String custName = "Steve Smith";

String firstName;

int spaceIdx;

// Get the index of the space character (" ") in custName.
spaceIdx = custName.indexOf(" ");
System.out.println("Index of space character (\" \") is : " + spaceIdx + "\n");
  
// Use the substring method parse out the first name and print it.
firstName = custName.substring(0,spaceIdx);
System.out.println("First Name is : " + firstName);
}
}

Output

Exercise 2

NameMaker.java

/

//To import classes from the util package
import java.util.*;
public class NameMaker {

   public static void main(String args[]){
  
       String firstName, middleName, lastName;
      
       // Declare the object and initialize with
// predefined standard input object
       Scanner reader = new Scanner(System.in);
       System.out.println("Enter a firstName: ");
       // String input
       firstName = reader.nextLine();
       System.out.println("Enter a middleName: ");
       // String input
       middleName = reader.nextLine();
      
       System.out.println("Enter a middleName: ");
       // String input
       lastName = reader.nextLine();
       //Close your scanner
       reader.close();
      
       //Concat all three first mid and lastName as
       String fullName = firstName + " " + middleName + " " + lastName ;

       System.out.println("Full Name is : "+ fullName + )" using + operator");
      
       //Using Concat method
       String fullName2=(new StringBuilder()).append(firstName).append(" ").append(middleName).append(" ").append(lastName).toString();
       System.out.println("Full Name is :("+ fullName2 + ") using concat method");
   }  
}

Output

EXERCISE 2

As while using the concatenate + operator you can concatenate A string with any data type.

like you can concat numbers with strings

example: String numberOfBats = 10 + " Bats";

OUTPUT here will be: 10 Bats

But a concatenate method could only be able to concatenate strings (i.e you can't concat strings with numbers etc.)

As in this question, All three user inputs are Strings. you can easily use either concat using append or use + operator without using the concat method.

Here I will prefer the + operator method.

Add a comment
Know the answer?
Add Answer to:
Java Homework Problems: 4. • Examine AddImport.java. – Perform the following: – Replace the fully qualified...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get...

    Exercise 1 • Examine ShoppingCart.java. – Perform the following: – Use the indexOf method to get the index for the space character (" ") within custName. Assign it to spaceIdx. – Use the substring method and spaceIdx to get the first name portion of custName. Assign it to firstName and print firstName. public class ShoppingCart {     public static void main (String[] args){         String custName = "Steve Smith";         String firstName;         int spaceIdx;                 // Get the...

  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • 8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private,...

    8.26 Encapsulate the Name class. Modify the existing code shown below to make its fields private, and add appropriate accessor methods to the class named getFirstName, getMiddleInitial, and getLastName. code: class Name { private String firstName; private String middleNames; private String lastName;    //Constructor method public Name(String firstName, String middleNames, String lastName) { this.firstName = firstName; this.middleNames = middleNames; this.lastName = lastName;    } //Accessor for firstName public String getFirstName() { return firstName; } //Accessor for middleNames public String getMiddlesNames()...

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String firstName;       String lastName; //answer goes here//    } }

  • java 1. Write a method header on line three with the following specs: Returns: a boolean...

    java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main {       {        return true;    } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main {       {    String ans = name.toUpperCase();...

  • 6. • Examine FlipCoin.java: – Execute the following program and observe the random number that chance...

    6. • Examine FlipCoin.java: – Execute the following program and observe the random number that chance generated. – If chance < 0.5, record the result as “heads”; else record the result as “tails.” – Repeat this many time. import java.util.Random; public class FlipCoin {     public static void main(String[] args) {         // 50% chance heads, 50% chance tails         Random rand = new Random();         double chance = rand.nextDouble();         System.out.println(chance);         }     } Exercise 2 • Examine...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT